1

我的问题是 Flask 上传无法提供文件名中带有希伯来字母的图像文件。这是我的表格:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, TextAreaField
from wtforms.validators import InputRequired, Length
from flask_wtf.file import FileField, FileAllowed
from flask_uploads import IMAGES



class RegisterForm(FlaskForm):
    name = StringField('Full name', validators=[InputRequired('A full name is required'),
                                                Length(max=100, message='your name can\'t be more than 100 characters')])
    username = StringField('Username', validators=[InputRequired('A username is required'),
                                                   Length(max=100, message='your username can\'t be more than 30 characters')])
    password = PasswordField('Password', validators=[
                             InputRequired('A password is required')])

    image = FileField(validators=[FileAllowed(IMAGES, 'Only images accepted')])

我检查了它,即使文件名中有希伯来字母,图像字段也已接受。但在视图中有一个问题。烧瓶上传配置为:

photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos) 

以及视图本身:

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        image_filename = photos.save(form.image.data)
        image_url = photos.url(image_filename)

        new_user = User(name=form.name.data, username=form.username.data,
                        password=generate_password_hash(form.password.data),
                        image=image_url, join_date=datetime.now())

        db.session.add(new_user)
        db.session.commit()

        return redirect(url_for('profile'))

    return render_template('register.html', form=form) 

如果文件名中有英文字母,则效果很好,但如果有希伯来字母,则该行有错误:

image_filename = photos.save(form.image.data)

错误 :

Traceback (most recent call last):
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/yosef/Desktop/twitee/views.py", line 97, in register
    image_filename = photos.save(form.image.data)
  File "/home/yosef/.local/share/virtualenvs/twitee-FE8NX-P8/lib/python3.8/site-packages/flask_uploads.py", line 415, in save
    raise UploadNotAllowed()

我该怎么办?

4

1 回答 1

2

不幸的是,目前不支持非 ascii 文件名。至少不在 PyPi 的打包版本中Flask-Uploads

于 2020-06-27T17:43:58.290 回答