Flask send_from_directory [英] Flask send_from_directory

查看:3162
本文介绍了Flask send_from_directory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flask网络应用程序,允许用户上传文件,然后下载或在浏览器中显示它们。我应该使用 send_from_directory 在生产环境中从我的服务器上的上传文件夹提供这些文件吗? 方案

我已经完成了这种想法。即时通讯使用烧瓶休息。但是你可以用它作为参考。
用于从document_folder.config中导入文件



import dir_path
file = data [文件']#数据是一个reqparse。
filename = werkzeug.secure_filename(file.filename)
file.save(os.path.join(str(dir_path),str(filename))


$ b config.py用于文档静态文件夹,将其放置在文档静态文件夹中
$ b

import os
dir_path = os.path.dirname(os.path.realpath(__ file __))


下载
您必须将文档的名称保存到您的分区,以便您可以下载文档。

docu = DocumentModel.exists(_id)
如果文件:
文件名= docu.filename
返回send_file(os.path.join(dir_path,filename),as_attachment = True)



解析器应该是这样的。

parser.add_argument('file ',
type = werkzeug.datastructures.FileStorage,
location ='files'


I have a Flask web app that allows users to upload files and then download or display them in a browser. Should I be using send_from_directory to serve these files from the upload folder on my server when in production?

解决方案

i have accomplished that kind of idea. im using flask-rest. But u can use this as reference. For uploading file

from document_folder.config import dir_path file = data['file'] #data is a reqparse. filename = werkzeug.secure_filename(file.filename) file.save(os.path.join(str(dir_path), str(filename))

config.py for document static folder. place this in the document static folder

import os dir_path = os.path.dirname(os.path.realpath(__file__))

for downloading You must save the document's name to your db so you can download the documents.

docu = DocumentModel.exists(_id) if docu: filename = docu.filename return send_file(os.path.join(dir_path, filename), as_attachment=True)

The parser should be like this.

parser.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files' )

这篇关于Flask send_from_directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆