Saving uploaded base64 data gives TypeError: a bytes-like object is required, not 'str'

Saving uploaded base64 data gives TypeError: a bytes-like object is required, not 'str'

我正在使用 JavaScript 库上传图片。它将图像数据放在一个表单字段中,该表单字段是一个 JSON 编码对象,具有文件名和 base64 编码数据。

当我尝试将图像保存在 Flask 中时,出现此错误:

builtins.TypeError TypeError: a bytes-like object is required, not 'str'

创建的图像文件名称正确,但数据已损坏,无法打开。如何保存上传的数据?

app = Flask(__name__)
UPLOAD_FOLDER = app.root_path + '/images/'

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        image_base = json.loads(request.form['file'])['output']['image']
        image_base = image_base[image_base.find(',')+1:]
        file_data = io.StringIO(image_base)
        file = FileStorage(file_data, filename=json.loads(from_form)['output']['name'])
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file', filename=filename))

    return render_template('index.html', methods=['GET', 'POST'])
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/kr/PycharmProjects/instaup/instaup.py", line 32, in index
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  File "/usr/local/lib/python3.5/site-packages/werkzeug/datastructures.py", line 2656, in save
    copyfileobj(self.stream, dst, buffer_size)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shutil.py", line 76, in copyfileobj
    fdst.write(buf)
TypeError: a bytes-like object is required, not 'str'

您需要将 app.config['UPLOAD_FOLDER']filename 编码为类字节对象。尝试 .encode('utf-8') 方法。

StringIO代表一个字符串,不是字节,你要BytesIO. Base64 encoded data is not the actual image bytes, it needs to be decoded with b64decode.

file_data = io.BytesIO(b64decode(image_base))