简体   繁体   English

简单文件上传不适用于Vue.js和Flask框架

[英]Simple File Upload not working with Vue.js & Flask framework

I'm having issues uploading an image via FormData from Vue.js to my Python flask backend. 我在通过FormData将图像从Vue.js上传到我的Python Flask后端时遇到问题。 I have a node.js server which handles Vue.js (Nuxt) so I can do SSR. 我有一个处理Vue.js(Nuxt)的node.js服务器,因此可以执行SSR。 The minimal stack setup: 最小堆栈设置:

Vue.js (Nuxt) frontend --> node.js proxy server ---> Python flask backend

HandleFile.vue

const formData = new FormData()
formData.append('image', file)
formData.append('data', JSON.stringify(upcomingReq))

const resp = await this.$axios.post('/api/receive-file', formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})

server.js (just a snippet of the proxy function from the node.js server which serves the nuxt app) server.js (仅是为nuxt应用程序提供服务的node.js服务器的代理功能的代码段)

app.use('/api', proxy({
  target: API_URI,
  changeOrigin: true,
  // logLevel: 'debug',
  onProxyReq(proxyReq, req, res) {
    if (req.session.authToken) {
      proxyReq.setHeader('Authorization', 'Bearer ' + req.session.authToken)
    }
  },
}))

app.py (the controller that receives the file) app.py (接收文件的控制器)

@v1.route('/api/receive-file', methods=['GET', 'POST'])
@auth_required
def receive_file():
    print('in here')
    return jsonify({'hi': 'ok'})

This is the error I'm getting: 这是我得到的错误:

在此处输入图片说明 ( 3000 is the Node server, 5000 is the flask server) 3000是节点服务器, 5000是烧瓶服务器)

Also, Flask is returning a 200 as if everything went ok. 另外,Flask会返回200 ,好像一切正​​常。 And upon inspecting the Flask request , the file is in there without a problem. 并且在检查了Flask request ,文件就在其中,没有问题。

I'm not sure why it seems the response is failing, or as the error says, the pipe becomes broken. 我不确定为什么响应似乎失败,或者如错误所示,管道损坏了。

For anyone that finds this looking for answers, it was resolved by running the localhost through ngrok . 对于任何找到此问题的人,可以通过ngrok运行localhost来解决

It seems there were headers missing, I believe an important one being Connection: keep-alive , when serving only via localhost. 似乎缺少标头,我相信一个重要的标头是Connection: keep-alive ,仅通过本地主机提供服务时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM