python实现文件上传(一种是flask实现,一种是tornado实现)
2021/10/15 17:14:30
本文主要是介绍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)
这篇关于python实现文件上传(一种是flask实现,一种是tornado实现)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python