web.py任意文件上传(Windows下)

web.py是一个python的web框架, 简单易用强大的功能. 以python的方式来写web.

在上传文件上, 我一直遇到点问题, 终于解决了, 记录在这里. 我在网上搜了很久, 这方面的资料好少, 希望可以帮助有需要的人, web.py还是很好用的.

以上都是参考了官方文档, 地址: http://webpy.org/cookbook/index.zh-cn

项目目录格式

web.py任意文件上传(Windows下)

说明: upload是上传文件的目录(win7下测试, 图片,文本等都正常), templates是html模板, sqlite.db是sqlite数据库文件, demo.py是源文件. run.bat和READE.txt没什么好说的.

demo.py源代码如下:

<font color="#0000ff">import</font> web
  
urls <font color="#0000ff">=</font> (
    <font color="#0000ff">'/'</font>, <font color="#0000ff">'index'</font>,
    <font color="#0000ff">'/add'</font>, <font color="#0000ff">'add'</font>,
    <font color="#0000ff">'/upload'</font>, <font color="#0000ff">'upload'</font>
)
  
<font color="#0000ff">class</font> index:
    <font color="#0000ff">def</font> GET(<font color="#808080">self</font>):
        todos <font color="#0000ff">=</font> db.select(<font color="#0000ff">'todo'</font>)
        <font color="#0000ff">return</font> render.index(todos)
  
<font color="#0000ff">class</font> add:
    <font color="#0000ff">def</font> POST(<font color="#808080">self</font>):
        i <font color="#0000ff">=</font> web.<font color="#ff1493">input</font>()
        n <font color="#0000ff">=</font> db.insert(<font color="#0000ff">'todo'</font>, <font color="#ff1493">id</font> <font color="#0000ff">=</font> i.<font color="#ff1493">id</font>, title <font color="#0000ff">=</font> i.title)
        <font color="#0000ff">raise</font> web.seeother(<font color="#0000ff">'/'</font>)
  
<font color="#0000ff">class</font> upload:
    <font color="#0000ff">def</font> POST(<font color="#808080">self</font>):
        x <font color="#0000ff">=</font> web.<font color="#ff1493">input</font>(myfile <font color="#0000ff">=</font> {})
        filedir <font color="#0000ff">=</font> <font color="#0000ff">'images'</font> <font color="#008200"># change this to the directory you want to store the file in. </font>
        <font color="#0000ff">if</font> <font color="#0000ff">'myfile'</font> <font color="#0000ff">in</font> x: <font color="#008200"># to check if the file-object is created </font>
            filepath <font color="#0000ff">=</font> x.myfile.filename.replace('\\','/') <font color="#008200"># replaces the windows-style slashes with linux ones. </font>
            filename <font color="#0000ff">=</font> filepath.split(<font color="#0000ff">'/'</font>)[<font color="#0000ff">-</font><font color="#009900">1</font>] <font color="#008200"># splits the and chooses the last part (the filename with extension) </font>
            fout <font color="#0000ff">=</font> <font color="#ff1493">open</font>(filedir +'/'+ filename,<font color="#0000ff">'wb'</font>) <font color="#008200"># creates the file where the uploaded file should be stored </font>
            fout.write(x.myfile.<font color="#ff1493">file</font>.read()) <font color="#008200"># writes the uploaded file to the newly created file. </font>
<font color="#008200">#            fout.write(x.myfile.value) # writes the uploaded file to the newly created file. </font>
            fout.close() <font color="#008200"># closes the file, upload complete. </font>
        <font color="#0000ff">raise</font> web.seeother(<font color="#0000ff">'/'</font>)
  
app <font color="#0000ff">=</font> web.application(urls, <font color="#ff1493">globals</font>())
render <font color="#0000ff">=</font> web.template.render(<font color="#0000ff">'templates/'</font>)
<font color="#008200">#db = web.database(dbn='sqlite', db=":memory:") </font>
db <font color="#0000ff">=</font> web.database(dbn='sqlite', db="sqlite.db")
  
conn <font color="#0000ff">=</font> db._db_cursor().connection
cursor <font color="#0000ff">=</font> conn.cursor()
cursor.execute(<font color="#0000ff">'DROP TABLE todo'</font>)
cursor.execute(<font color="#008200">''' </font>
<font color="#008200">CREATE TABLE todo ( </font>
    <font color="#008200">id text primary key, </font>
    <font color="#008200">title text, </font>
    <font color="#008200">created timestamp, </font>
    <font color="#008200">done boolean </font>
<font color="#008200">) </font>
<font color="#008200">'''</font>)
  
cursor.execute(<font color="#008200">''' </font>
<font color="#008200">INSERT INTO todo (id, title) VALUES ('01', 'Learn web.py') </font>
<font color="#008200">'''</font>)
  
cursor.execute(<font color="#008200">''' </font>
<font color="#008200">INSERT INTO todo (id, title) VALUES ('02', 'Learn python') </font>
<font color="#008200">'''</font>)
  
conn.commit()
  
<font color="#0000ff">if</font> __name__ == <font color="#0000ff">"__main__"</font>:
    app.run()

上述的代码关键的地方, fout = open(filedir +'/'+ filename,'wb')

官方文档给的例子是打开的模式是'w', 但是下面有一行说明:

[事实上,一定要以"mb"模式打开文件(在windows下), 也就是二进制可写模式, 否则图片将无法上传。]

我之前用w试着运行, 文本文件可以上传, 但是图片等文件, 就会文件错误, 于是修改成二进制文件的方式, 之后就正常了.

源代码下载:

具体下载目录在 /pub/2011/12/12/web.py任意文件上传(Windows下)/

相关推荐