Flask - uploadnotallowed错误 - 重命名要保存的文件时

时间:2016-09-07 21:29:25

标签: python flask

我正在尝试上传一个excel文件,并在保存时给它一个新名称,例如:oldname.xlsxnewname.xlsx

到目前为止,这是我的代码:

from flask import Flask, render_template, send_file, request, redirect, url_for
from flask_uploads import UploadSet, configure_uploads, DOCUMENTS, IMAGES
from remove_characters import get_csv, edit_data, cleanup_data
import re
import os

app = Flask(__name__)

#the name 'datafiles' must match in app.config to DATAFILES
docs = UploadSet('datafiles', DOCUMENTS)
app.config['UPLOADED_DATAFILES_DEST'] = 'static/uploads'
configure_uploads(app, docs)
file_new_name = 'dataexcel'

@app.route("/upload", methods = ['GET', 'POST'])
def upload():
#user_file is the name value in input element
if request.method == 'POST' and 'user_file' in request.files:
    filestorage = request.files['user_file']
    path = "static/uploads/" + filestorage.filename
    filename = docs.save(filestorage, name = file_new_name)


    return redirect(url_for('results', path = path))


return render_template('upload.html')

所以在save函数中,我将file_new_name传递给名称参数,因此它将使用该变量名保存。我从flask上传文档中获得了name param,但是我收到了'uploadnotallowed'错误

enter image description here

我想知道如果我没有遵循save功能的正确格式,或者我的配置设置不正确。我是新手,所以我还在学习这个很酷的网页框架。提前致谢

1 个答案:

答案 0 :(得分:0)

好的,发现了我的错误。变量file_new_name = 'dataexcel'需要具有扩展名,在本例中为.xlsx分机。所以变量应该是file_new_name = 'dataexcel.xlsx'

save函数应如下所示 - > filename = docs.save(filestorage, None, file_new_name)None是子文件夹,如果您想传递子文件夹,只需将其更改为static/upload/dist