python实现文件上传(一种是flask实现,一种是tornado实现)

flask实现文件的上传

#flask实现文件上传
@app.route("/filetranslate", methods=['POST'])
def filetranslate():
    files = request.files["files"]
    print(files, 12901)
    print(files.filename)
    file = "static/files/" + files.filename
    print(file)
    files.save(file)
    url = urls + '/filetranslate'
    param = {'files': open(file, 'rb')}
    rst = requests.post(url, files=param).content
    try:
        res = json.loads(rst)
    except Exception as e:
        res = {
            'code': -1,
            'message': e
        }
    return res

tornado实现文件上传:

# 文件翻译
class FiletranslateHandler(tornado.web.RequestHandler):
    def post(self):
        relative_file = self.request.files["files"]
        for i in relative_file:
            title = i["filename"]
            print(title)
            file_content = i["body"]
            content_type = i["content_type"]  # 文件类型
            print(content_type)
            if title.find(".docx") != -1:
                filepath = "static/translateFile/{}".format(title)
                file = open(filepath, 'wb+')
                file.write(file_content)
                file.close()
                data = tools.uploadFile(title, content_type, filepath)
            else:
                data = {
                    "code": 3,
                    "message": "文件不能解析"
                }
            tools.corsHandler(self)
            self.finish(data)

 

上一篇:128. 最长连续序列


下一篇:Hello WEB! AJAX yyds(二)--前后端交互