我有以下文件结构:
.
├── app
│ ├── api_routes
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── __init__.py
│ ├── main_routes
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── models.py
│ ├── static
│ │ └── styles.css
│ ├── templates
│ │ └── base.html
│ └── uploads
│ └── 10_0_0.jpg
├── application.py
└── config.py在我的config.py中,我有以下内容:
class Config(object):
UPLOAD_FOLDER = 'uploads/'当我保存一个上传的文件,然后将其发送回我正在使用的用户(仅作为示例)时:
fname = 'foo.jpg'
fname_save = os.path.join(current_app.root_path, current_app.config['UPLOAD_FOLDER'], fname)
fname_retr = os.path.join(current_app.config['UPLOAD_FOLDER'], fname)
file.save(fname_save)
return send_from_directory(os.path.dirname(fname_retr),
os.path.basename(fname_retr))cwd中的上传文件夹(保存文件的位置)和flask模块正在运行的文件夹(app/)具有不同的名称,这有点单调乏味。有没有比我现在的解决方案更好的方案来解决这个问题呢?
发布于 2018-08-01 17:22:31
我会这样做:
@app.route('/upload', methods=['POST'])
def myroute():
fname = 'foo.jpg'
file = request.file[0] # not save at all
send_back_file = io.BytesIO(file.read())
file.seek(0)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], fname))
return send_file(send_back_file, attachment_filename=fname, as_attachement=True)资源:
https://stackoverflow.com/questions/49945304
复制相似问题