web.py檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   檔案   2014   

程式如下:

import webimport cgicgi.maxlen = 10 * 1024 * 1024 # 10MB#http://192.168.2.125:8080/uploadurls = (‘/upload‘, ‘upload‘)render = web.template.render(‘templates/‘)fmap = {  "f1": "/tmp/upload1",  "f2": "/tmp/upload2"}class upload:    def GET(self):        fid = web.input(fid="").fid         web.header("Content-Type","text/html; charset=utf-8")        return render.upload(fid)     def POST(self):        try:            x = web.input(myfile={})            #web.debug(x[‘myfile‘].filename)            #web.debug(x[‘myfile‘].value)            #web.debug(x[‘myfile‘].file.read())            #filedir = ‘/root/python/web.py-0.33/uploadfile‘ # /home/zcw/python/web_chanelpack/web.py-0.33/uploadfile2            if ‘myfile‘ in x: # to check if the file-object is created                filepath=x.myfile.filename.replace(‘\\‘,‘/‘) # replaces the windows-style slashes with linux ones.                filename=filepath.split(‘/‘)[-1] # splits the and chooses the last part (the filename with extension)                                fid = web.input(fid="").fid                fpath = fmap[fid]                print "fpath: %s" % fpath                fout = open(fpath +‘/‘+ filename,‘w‘) # creates the file where the uploaded file should be stored                fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.                fout.close() # closes the file, upload complete.            raise web.seeother(‘/upload?fid=%s‘ % fid)        except ValueError:            return "File too large"if __name__ == "__main__":   app = web.application(urls, globals())    app.run()
View Code

效果:

 

目錄結構:

啟動過程:

sudo python setup.py install
sudo python upload.py 5987 &
訪問:http://127.0.0.1:5987/upload?fid=f2

 web.py:http://webpy.org/static/web.py-0.33.tar.gz

web.py檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.