zoukankan      html  css  js  c++  java
  • web.py处理文件上传

     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 created
    23             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 stored
    26             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()
  • 相关阅读:
    利用crontab每天定时备份MySQL数据库
    MySQL基本SQL语句之数据插入、删除数据和更新数据
    Vim命令合集
    ubuntu下手动安装php-amqp模块教程
    变量作用域(总结篇)
    变量作用域(示例篇)
    定义一个函数
    正则表达式
    re模块中常用功能函数
    python内置函数
  • 原文地址:https://www.cnblogs.com/catmelo/p/3898357.html
Copyright © 2011-2022 走看看