D
D
Danil2015-05-22 10:57:46
Node.js
Danil, 2015-05-22 10:57:46

Why is file upload not working in nodejs via multer?

I'm trying to upload pictures.
My router:

upload.post('/', function(req, res) {

    console.log(req.body)
    console.log(req.files)

});

My form:
<form id="photo-form" name="upload" enctype="multipart/form-data" method="post" action="/upload">
        <label>
            <input id="files-field" type="file" multiple="multiple" />
        </label>
            <hr>
            <input type="submit">
</form>

In app.js I have:
var app = express();
var multer  = require('multer')
app.use(multer({ dest: './uploads'}))

As a result req.body, req.fileI have {}, that is, nothing comes. What could be the problem?
UPD Issue resolved. Is there another option with jQuery?
var items = $('#files-field')

    $.ajax({
      type: "POST",
      url: '/upload',
      data: items,
      processData: false,
      dataType: 'multipart/form-data',
      success: function(){
        window.location.href = '/';
      }
    });

In the console I get {'[object Object]':' '}and there are no files. What's wrong here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-05-22
@Veneomin

<input name="???"
to upd:
read what is jQ data
data: $('#photo-form').serialize()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question