我正在尝试让http://fb4demo.bitplan.com/upload使用以下代码:
class UploadForm(FlaskForm):
'''
upload form example
'''
file = MultipleFileField('File(s) to Upload')
submit = SubmitField()
{% extends 'base.html' %}
{% from 'bootstrap/form.html' import render_form, render_field, render_form_row %}
{% block content %}
<!-- https://github.com/greyli/flask-dropzone/issues/29 -->
<script type="text/javascript">
Dropzone.options.myDropzone.headers = {"X-CSRF-Token": "{{ csrf_token() }}"}
</script>
<h1>Upload Form</h1>
{{ render_form(upload_form) }}
{{ dropzone.create(action=url_for('test_upload')) }}
{{ dropzone.config() }}
{% endblock %}
def upload(self):
'''
handle the uploading
'''
form= UploadForm()
filenames=""
delim=""
if form.validate_on_submit():
for file in form.file.data:
file_filename = secure_filename(file.filename)
filePath=f'/tmp/{file_filename}'
with open(filePath, 'wb') as f:
f.write(file.read())
size=os.path.getsize(filePath)
filenames=f"{filenames}{delim}{file_filename}({size})"
delim="<br/>"
flash(Markup(filenames))
return render_template('upload.html',upload_form=form)
拖放工作除了 问题 #147 CSRF 令牌处理不清楚但单击提交时文件名不可用。
相反,Filestorage 对象是类型
str: application/octet-stream
如何正确检索文件?