【问题标题】:Saving uploaded base64 data gives TypeError: a bytes-like object is required, not 'str'保存上传的 base64 数据会导致 TypeError: a bytes-like object is required, not 'str'
【发布时间】:2017-09-05 16:56:08
【问题描述】:

我正在使用 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'

【问题讨论】:

    标签: python python-3.x flask base64 werkzeug


    【解决方案1】:

    StringIO 代表一个字符串,而不是字节,你想要BytesIO。 Base64编码的数据不是实际的图像字节,需要用b64decode解码。

    file_data = io.BytesIO(b64decode(image_base))
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如果添加其中一个,得到这个:TypeError: Can't mix strings and bytes in path components,如果两个 if,得到这个:AttributeError: 'bytes' object has no attribute'写'
      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 2017-11-04
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多