axios 无法从flask服务器向reaction服务器发送回映像

iyfamqjs  于 2023-02-04  发布在  iOS
关注(0)|答案(1)|浏览(163)

因此,我使用axios post请求从react向flask发送了一个图像,该图像采用formdata格式

const response = await axios({
      method: "POST",
      url: "http://127.0.0.1:5000/upload",
      headers: { "content-type": "multipart/form-data" },
      data: filou,

在 flask 回来,我收到图像,存储它,然后作为一个响应发送另一个图像

@app.route('/upload', methods=['POST'])
@cross_origin()
def fileUpload():
    print("hello")
    
    print(request.files)
    
    # check if the post request has the file part
    if 'file' not in request.files:
        print("no file")
        flash('No file part')
        return "redirect(request.url)"
    file = request.files['file']
    
    # If the user does not select a file, the browser submits an
    # empty file without a filename.
    if file.filename == '':
        print("no selected file")
        flash('No selected file')
        return redirect(request.url)
    
    if file and allowed_file(file.filename):
        print("\n YYYYYYYYYYYYYYYYYYYEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEYYYY \n")
        print(file.filename)
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # print(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        new_filename = app.config['UPLOAD_FOLDER']
        print(new_filename)
        
        return send_file(f"output/final_output/panda.jpg")

当我收到图像,我得到它像这样
enter image description here
但是我不能在react中显示它,我不知道如何做(我尝试了许多技术,blob,createObjectURL ...等等)
一个二个一个一个
如果有人能帮忙。

dzhpxtsq

dzhpxtsq1#

就像Unmitigated说的...

axios
.get('your_url',{ responseType: 'blob' })
.then((response) => {
      const img = URL.createObjectURL(response.data);
      setoutImage(img);
      ...
    });

相关问题