博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web.py Cookbook 简体中文版 - 保存上传的文件
阅读量:4650 次
发布时间:2019-06-09

本文共 1251 字,大约阅读时间需要 4 分钟。

问题

上传文件,并将其保存到预先设定的某个目录下。

方法

import weburls = ('/upload', 'Upload')class Upload:    def GET(self):        web.header("Content-Type","text/html; charset=utf-8")        return """
""" def POST(self): x = web.input(myfile={}) filedir = '/path/where/you/want/to/save' # change this to the directory you want to store the file in. 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) fout = open(filedir +'/'+ 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')if __name__ == "__main__": app = web.application(urls, globals()) app.run()

Hang ups

同时还需要注意如下几点:

  • 转到 。 * 千万不要让用户把文件上传到那些不经过文件后缀和类型检查而执行文件的文件夹下。
  • 事实上,一定要以”mb”模式打开文件(在windows下), 也就是二进制可写模式, 否则图片将无法上传。

转载于:https://www.cnblogs.com/justjavac/archive/2012/11/23/webpy-cookbook-storeupload.html

你可能感兴趣的文章
结构体的传参理解成员的存储方式
查看>>
python 进程与线程(理论部分)
查看>>
什么是API
查看>>
Java反射中method.isBridge() 桥接方法
查看>>
[shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
查看>>
强名称程序集(strong name assembly)——为程序集赋予强名称
查看>>
1028. List Sorting (25)
查看>>
BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
查看>>
ubuntu 重启命令,ubuntu 重启网卡方法
查看>>
Linux的学习:
查看>>
JavaScript中的原型继承原理
查看>>
Python logger模块
查看>>
jquery控制css的display(控制元素的显示与隐藏)
查看>>
关于python做人工智能的一个网页(很牛逼)
查看>>
判断控件的CGRect是否重合,获取控件的最大XY值
查看>>
POJ-1128 Frame Stacking
查看>>
python第三十九课——面向对象(二)之初始化属性
查看>>
GET请求在Tomcat中的传递及URI传递
查看>>
JavaScript 复杂判断的更优雅写法借鉴
查看>>
<mvc:annotation-driven/>浅析
查看>>