roccomuso / iot-433mhz

:globe_with_meridians: IoT System to control 433 MHz RC power sockets, PIR, Door Sensors and much more.
Other
332 stars 47 forks source link

File upload with Multer. #6

Open roccomuso opened 8 years ago

roccomuso commented 8 years ago

Card header image uploaded as temporary Image even if a card is rejected during validation.

"If req.body is empty, it means that the client sent the file before sending the new_file_name field.
There is nothing we can do about it in multer."

See https://github.com/expressjs/multer/issues/299 and https://github.com/expressjs/multer/issues/146

Tyrone2333 commented 6 years ago

In my Request payload:

------WebKitFormBoundary2xMRe7hq1ThhErY2 Content-Disposition: form-data; name="file"; filename="qr.png" Content-Type: image/png ------WebKitFormBoundary2xMRe7hq1ThhErY2 Content-Disposition: form-data; name="author" root ------WebKitFormBoundary2xMRe7hq1ThhErY2 Content-Disposition: form-data; name="username" root ------WebKitFormBoundary2xMRe7hq1ThhErY2--

The file was sent before the body,so I can't got req.body in multer.diskStorage, I tried everything,finally I found create new FormData,then use axios to send it to Server could got req.body.My code:

 let formData = new FormData()
 formData.append("author", _this.$store.state.user.user.username)
 formData.append("type", "article")
 formData.append("file", $file)  // Put it at the bottom
 axios({
        url: process.env.BASE_API + "/article/upload_img",
        method: 'post',
        data: formData,
        headers: {'Content-Type': 'multipart/form-data', 'token': token},
  }).then((res) => { }

New Request payload:

------WebKitFormBoundaryEbY70DntTXeXcwri Content-Disposition: form-data; name="author" root ------WebKitFormBoundaryEbY70DntTXeXcwri Content-Disposition: form-data; name="type" article ------WebKitFormBoundaryEbY70DntTXeXcwri Content-Disposition: form-data; name="file"; filename="qr.png" Content-Type: image/png ------WebKitFormBoundaryEbY70DntTXeXcwri--

I hope this is useful to you.

Great-hijack commented 4 years ago

This is so helpful for me. It's working well.