我有一个项目,允许用户上传文件,他们最终将得到处理,然后提供结果文件。
我刚刚意识到,现在我正在尝试实现系统的处理部分,上载时文件为空,而且我不确定这哪里出错了。文件已成功命名并放置在目录中,但为空。
任何帮助将不胜感激。
用于上传的html表单:
<div class="tile is-vertical is-parent coach" id='fileUploadTile'>
<div class="tile is-child box has-background-light">
<form action='/' method='POST' enctype="multipart/form-data">
<div class='drop-zone'>
<span class='drop-zone__prompt is-family-monospace'>Drag and drop or click here to upload files</span>
<input class="drop-zone__input" type="file" multiple name="uploaded_file" id='fileLoader'>
</div> <!-- end of drop-zone-->
<div class='buttons are-medium' id='fileButtons'>
<input type='submit' class="button is-success is-light is-outlined is-family-monospace" id='submitFiles' value='Process file(s)'>
</form>
<button class='button is-danger is-light is-outlined is-family-monospace' name='resetFiles' id='resetFiles'>Reset File(s)</button>
</div> <!-- end of buttons -->
</div> <!-- end of tile is-child -->
</div> <!-- end of tile is-vertical -->
试图验证文件(检查扩展名,文件大小,名称等)然后保存的代码:
def file_upload():
if session:
session_name = session.get('public_user')
print("New request from " + str(session_name))
# update when last request was sent
active_sessions[session_name] = datetime.now()
else:
session_token = generate_session_token()
print("Generating new session... " + session_token)
session['public_user'] = session_token # session = key, last request = value
active_sessions[session_token] = datetime.now()
os.mkdir('uploads/'+session['public_user']) #create directory for uploaded files
if request.method == "POST":
if request.files:
files_processed = True
files = request.files.getlist("uploaded_file")
upload_path = app.config['UPLOAD_FOLDER'] + \
str(session['public_user'])
# loop, as possibility of multiple file uploads
for file_to_upload in files:
file_to_upload.seek(0, os.SEEK_END)
file_length = file_to_upload.tell()
file_name = check_existing_file_name(file_to_upload.filename)
# Secures file name against user input
file_name = secure_filename(file_name)
# Checks the file name isn't blank
if file_to_upload.filename == "":
print("Error with file" + file_to_upload.filename +
" - name must not be blank")
files_processed = False
continue
# Checks the file has an allowed extension
elif not allowed_ext(file_to_upload.filename):
print("Error with file" + file_to_upload.filename +
" - extension not supported")
files_processed = False
continue
# Checks file size
elif file_length > app.config['MAX_FILE_SIZE']:
print("Error with file" +
file_to_upload.filename + " file too big")
files_processed = False
continue
else: # Else, passes all validation and is saved.
file_path = upload_path + "/" + file_name
file_to_upload.save(file_path)
# If files have been processed, return a render with success message
if files_processed is True:
return render_template('index.html', is_home='yes', succ="Now processing your files...")
else: # Else, normal redirect.
return redirect(request.url)
else: # If no files request, redirect to index.
return redirect(request.url)
else: # If not a POST request, load page as normal.
return render_template('index.html', is_home='yes')
抱歉,这是我错过的简单或愚蠢的事情-这是我的第一个Python项目,也是我第一次使用Flask。
答案 0 :(得分:2)
一切看起来都还可以。
不相关:我将根据file_name
而不是file_to_upload.filename
检查空文件名。
关于您的问题:
唯一明智的答案可能是,通过执行许多操作,您都将文件指针放在文件末尾,而save
无法处理它。
很遗憾,我没有时间在我的电脑上尝试此操作,因此请再次执行seek 0
-这次是在调用save
方法之前。
我将稍后再尝试并相应地更新答案。
找出正在发生的事情的另一种方法是使用调试器。不太复杂。
我制作了一个5分钟的视频-仅用于调试Flask:https://www.youtube.com/watch?v=DB4peJ1Lm2M
答案 1 :(得分:0)
我遇到了同样的问题,上面推荐的修复程序对我有所帮助!据我了解,这是因为当您抓住文件大小时,文件指针位于文件的末尾。只需与另一个file_to_upload.seek(0)
撤消。
为清楚起见,您的代码现在应如下所示:
file_path = upload_path + "/" + file_name
file_to_upload.seek(0)
file_to_upload.save(file_path)