V
V
Viktor Zernov2016-06-03 23:49:24
JavaScript
Viktor Zernov, 2016-06-03 23:49:24

File upload express/multi. Does the request work successfully only in POSTMAN?

Hello! Faced such problem when uploading a file to the server. I use express.js along with multer. The essence of the problem is that using POSTMAN the request is executed and the file is successfully sent to the server. But when you do everything simply through the form on the page, the request is executed, but the file is not uploaded to the server. What did I screw up? Any ideas?

multer = require('multer')
upload = multer({ dest: 'uploads/' })

editor.post '/import',upload.single('import'), (req, res) ->
    session = req.session
    if typeof session.userId == 'undefined'
      res.redirect 'http://localhost:' + config.PORT + '/auth'
      res.end()

    console.log req.body.import
    console.log req.file
    res.end()

<form method='post' action='/editor/import' enctype='multipart/form-data'>
   <input type='file' name="import" />
   <button type="submit">Импортировать</button>
</form>

da5d1a29038c4caaad388bdea73bf867.pngConsole output(POSTMAN)
undefined
{ fieldname: 'import',
originalname: 'Test 2.json',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'uploads/',
filename: '0d787c6925eeb3895af98f11e500cfe8' ,
path: 'uploads\\0d787c6925eeb3895af98f11e500cfe8',
size: 1612 }
Console output(Form on page)
Test 2.json
undefined
HTTP (POSTMAN)
POST /editor/import HTTP/1.1
Host: localhost:8080
Cache-Control: no-cache
Postman-Token: 832bb0f7-de31-38ae-1bbb-16bc3b4a7c92
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="import"; filename=""
Content-Type:
----WebKitFormBoundary7MA4YWxkTrZu0gW
HTTP (From client)
{ host: 'localhost:8080',
connection: 'keep-alive',
'content-length': '38',
'cache-control' : 'max-age=0',
origin: ' localhost:8080 ',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ( KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
accept: 'text/html,
referer: ' localhost:8080/editor ',
'accept-encoding': 'gzip, deflate',
'accept-language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q =0.4',
cookie: 'connect.sid=s%3AW_Z9Gn_BXYgk5OlhrrEBtWfmPvpMAEi8.bfeXwChDbgz3bQtqc%2BtGjZ3InC5SohDgqxfZjh3ipWw' }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Zernov, 2016-06-04
@BeSa1nt

In development, I use React.js. The problem was caused by React ignoring the enctype="multipart/form-data" attribute when rendering. The solution to this problem is to change the name of the attribute from ecntype to encType.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question