我正在尝试进行简单的文件上传并收到错误: kombu.exceptions.EncodeError:FileStorage 类型的对象不可 JSON 序列化
我的代码:
@flask_app.post('/upload')
def upload_post():
uploaded_file = request.files['file']
if uploaded_file.filename != '':
result = upload_file_task.delay(uploaded_file)
return {"result_id": result.id}
#return jsonify({'status':'OK', 'filename': uploaded_file.filename})
else:
return jsonify({'status':'No file'})
@shared_task(bind=True, ignore_result=False)
def upload_file_task(self, file):
try:
# Save the file
filename = secure_filename(file.filename)
file.save(os.path.join('tmp/', filename))
return 'File uploaded successfully'
except Exception as exc:
raise self.retry(exc=exc)
我尝试将 serializer = 'pickle' 添加到任务注释中,但随后出现此错误: kombu.exceptions.EncodeError:无法腌制“_io.BufferedRandom”对象
解决方法是对对象进行编码并将其作为 JSON 传递给 Celery 任务,然后在任务中对其进行解码。我按照这段代码如何在Django中使用Celery上传文件