无法通过连接我前端的 REST API 检索图像和视频

问题描述

我尝试以表单形式从用户那里获取视频和图像,单击“提交”后,视频和图像将转换为 Blob 并发送到后端。令我惊讶的是,blob 文件无法显示,每当我使用时它都会返回 NONE

print(request.form.get('video'))

我的 demo.html :

<label>Choose video</label>
<input id="video" type="file" accept="video/*"><br><br>
<label>Choose thumbnail</label>
<input id="image" type="file" accept="image/*"><br><br>
<button onclick="submit(event)">Submit</button>

<script>
  function submit($event) {
  var formData = new FormData()

  videoFileType = document.getElementById("video").files[0].type
  imageFileType = document.getElementById("video").files[0].type

  videoBlob = new Blob([document.getElementById("video").files[0]],{type: videoFileType});
  imageBlob = new Blob([document.getElementById("image").files[0]],{type: imageFileType});

  formData.append('video',videoBlob)
  formData.append('image',imageBlob)

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log('Data `enter code here`submission successful.');
    }
  };
  xhttp.open("POST","http://127.0.0.1:9000/abc",true);
  xhttp.send(formData);
}

</script>

main.py(在服务器端)

from flask import Flask,render_template
from flask import request,jsonify

app = Flask(__name__)

log_file = None

@app.route('/qaa',methods=['GET'])
def index():
    return render_template('demo.html')

@app.route('/abc',methods=['GET','POST'])
def getvalue():
    log_file = request.form
    print(request)
    video = request.form.get('video')


    if log_file is not None:
        print("**********")
        print(request.form)
        print(request.form.get('video'))
        
        # log_file variable will have all the information
        # from the JSON log file
        

    else:
        return "No data provided",400



if __name__ == '__main__':
    app.run(port=9000)[enter image description here][1]

前端没有错误,因为它以二进制形式发送视频和图像。

请提出解决方法或后端所需的任何编辑。

my output

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)