如何在使用FlaskForm的表单中获取输入文件的MIME类型?

huangapple go评论92阅读模式
英文:

How to get mimetype of the input file in a form using FlaskForm?

问题

我正在尝试使用Flask和flask_wtf构建文件上传算法来处理上传表单。我的问题是如何获取输入文件的MIME类型。

  1. class FileUploadForm(FlaskForm):
  2. file = FileField('文件', validators=[DataRequired(), Length(min=1)])
  3. submit = SubmitField('上传')
  4. @blueprint.route('/upload/', methods=['GET', 'POST'])
  5. def upload():
  6. form = FileUploadForm()
  7. if form.validate_on_submit():
  8. new_file = File(secure_filename(form.file.name),
  9. form.file.read(),
  10. mimetype=# 在这里我需要传递MIME类型)
  11. db.session.add(new_file)
  12. db.session.commit()
  13. flash('文件上传成功!', 'success')
  14. return redirect(url_for('view', file_id=new_file.id))
  15. return render_template('files/upload.html', title='上传文件')

注意:在代码中的"mimetype=# 在这里我需要传递MIME类型"部分,你需要补充获取MIME类型的代码。获取MIME类型的方法通常是使用Python的mimetypes模块或通过检查文件扩展名来推断。

英文:

I am trying to build a file upload algorithm in flask using flask_wtf for handling upload form.
My question is how do I get the mimetype of the input file.

  1. class FileUploadForm(FlaskForm):
  2. file = FileField('File', validators=[DataRequired(), Length(min=1)])
  3. submit = SubmitField('Upload')
  4. @blueprint.route('/upload/', methods=['GET', 'POST'])
  5. def upload():
  6. form = FileUploadForm()
  7. if form.validate_on_submit():
  8. new_file = File(secure_filename(form.file.name),
  9. form.file.read()
  10. mimetype=# HERE I NEED TO PASS THE MIMETYPE)
  11. db.session.add(new_file)
  12. db.session.commit()
  13. flash('File uploaded successfully !', 'success')
  14. return redirect(url_for('view', file_id=new_file.id))
  15. return render_template('files/upload.html', title='Upload Files')

答案1

得分: 0

  1. class FileUploadForm(FlaskForm):
  2. file = FileField('选择文件', validators=[FileRequired()])
  3. submit = SubmitField('上传')
  4. @blueprint.route('/upload/', methods=['GET', 'POST'])
  5. def upload():
  6. form = FileUploadForm()
  7. if form.validate_on_submit():
  8. secure_name = secure_filename(form.file.data.filename)
  9. new_file = File(secure_name, form.file.data.stream.read(),
  10. mime_type=form.file.data.mimetype,
  11. length=form.file.data.stream.tell())
  12. db.session.add(new_file)
  13. db.session.commit()
  14. flash('文件上传成功!', 'success')
  15. return redirect(url_for('manager.files.view', file_id=new_file.id))
  16. return render_template('files/upload.html', form=form, title='上传文件')
英文:

Okay I got it.
Here is the code for people wanting it.

  1. class FileUploadForm(FlaskForm):
  2. file = FileField('Choose File', validators=[FileRequired()])
  3. submit = SubmitField('Upload')
  4. @blueprint.route('/upload/', methods=['GET', 'POST'])
  5. def upload():
  6. form = FileUploadForm()
  7. if form.validate_on_submit():
  8. secure_name = secure_filename(form.file.data.filename)
  9. new_file = File(secure_name, form.file.data.stream.read(),
  10. mime_type=form.file.data.mimetype,
  11. length=form.file.data.stream.tell())
  12. db.session.add(new_file)
  13. db.session.commit()
  14. flash('File uploaded successfully !', 'success')
  15. return redirect(url_for('manager.files.view', file_id=new_file.id))
  16. return render_template('files/upload.html', form=form, title='Upload Files')

huangapple
  • 本文由 发表于 2020年1月6日 18:44:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定