web.py處理檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   檔案   for   art   

 1 #coding=utf8 2 import web 3  4 urls = (‘/‘,‘Home‘, 5         ‘/upload‘, ‘Upload‘) 6  7 app = web.application(urls, globals())  8  9 class Upload:10     def GET(self):11         web.header("Content-Type","text/html; charset=utf-8")12         return """<html><head><title>Upload</title></head><body>13 <form method="POST" enctype="multipart/form-data" action="">14 <input type="file" name="myfile" />15 <br/>16 <input type="submit" />17 </form>18 </body></html>"""19     def POST(self):20         x = web.input(myfile={})21         filedir = ‘./‘ # change this to the directory you want to store the file in.22         if ‘myfile‘ in x: # to check if the file-object is created23             filepath=x.myfile.filename.replace(‘\\‘,‘/‘) # replaces the windows-style slashes with linux ones.24             filename="thisismy.file" # splits the and chooses the last part (the filename with extension)25             fout = open(filedir +‘/‘+ filename,‘w‘) # creates the file where the uploaded file should be stored26             fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.27             fout.close() # closes the file, upload complete.28         raise web.seeother(‘/upload‘)29 30 if __name__ == "__main__":31     app.run()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.