我使用Flask将文件上传到服务器,当我上传时,没有出错,也没有任何错误,一旦文件上传,数据库也被修改,但是当上传完成后,我点击了文件链接,发现服务器上没有相应的文件,因此下载过程失败。
实际上,上传后有一个判断,只有当文件成功保存成功才会写入数据库,但现在结果却是相反的,所以我不知道它出错了。有人可以看看吗?
status = upload_file(curr_upload, upload_path, ['wrap', 'pdf'])
if status[0]:
homework_upload = HomeworkUpload(name=homework_file_name, homework_id=hid, user_id=current_user.id, uri=homework_uri)
db.session.add(homework_upload)
cur_homework.uploads.append(homework_upload)
db.session.commit()
upload_ids.append(homework_upload.id)
upload_names.append(homework_upload.name)
upload_uris.append(homework_upload.uri)
upload_times.append(datetime.strftime(homework_upload.submit_time, '%Y-%m-%d %H:%M'))
def upload_file(file_src, des_path, allowed_type=None):
if file_src.filename == '':
message = gettext('No selected file')
return False, message
extension = file_src.filename[file_src.filename.rfind('.')+1:]
file_type = extension_to_file_type(extension)
folder = des_path[:des_path.rfind('/')]
if not allowed_type:
allowed_type = current_app.config['ALLOWED_RESOURCE_TYPE']
if file_src and '.' in file_src.filename and file_type in allowed_type:
if not os.path.exists(folder):
os.makedirs(folder)
file_src.save(des_path)
unique_uri = os.stat(des_path).st_mtime
if os.path.exists(des_path):
return True, unique_uri
else:
message = gettext("Save File Failed!")
return False, message
else:
message = gettext("Invalid file")
return False, message