4

I'm developing a web application using nodejs server-side. I'm trying to send pdf files from client to server.

Client:

var files = new FormData();
var count = 0;
$('#tableSlideId tr').each(function() {

    var inputForm = $(this).find("th:first").children();
    file = inputForm[0].files[0];
    files.append((count++).toString(),file);

});

$.ajax({
    type: "POST",
    url: "/sendFiles",
    data: files,
    contentType: false,
    processData: false,

}).done(function(err){

    var text ="";
    if(err) {

        text = "Upload FAILED! Retry ...";

    } else {

        text = "Upload SUCCES!";

    }

    alert(text);



});

I think the client side is ok, infact if I use this loop:

for(var p of files)
  console.log(p);

I correctly visualize all the elements that I want to send to the server.

Server:

app.post('/sendFiles', function(req,res) {

    console.log("--->",req.body);
    res.end();

});

Now in the server I have no idea how to visualize the data that I send, infact req.body is empty.

I don't know if this is the right way but my goal is to load some pdf files form the client, send to the server and after store them in a mySql dmbs. Thank you.

Giorgio Di Rocco
  • 119
  • 1
  • 2
  • 7

2 Answers2

3

Use express-formidable module. install 'express-formidable' by the running command

npm install express-formidable --save

a simple example is as follows from github


const express = require('express');
const formidable = require('express-formidable');

var app = express();

app.use(formidable());

app.post('/upload', (req, res) => {
  //req.fields contains non-file fields 
  //req.files contains files 
  console.log(req.fields);
  console.log(req.files);
});

Hope this helps!

Edit, from here -

app.post('/submit-form', (req, res) => {
  new formidable.IncomingForm().parse(req, (err, fields, files) => {
    if (err) {
      console.error('Error', err)
      throw err
    }
    console.log('Fields', fields)
    console.log('Files', files)
    files.map(file => {
      console.log(file)
    })
  })
})
Darshan Devrai
  • 566
  • 5
  • 10
  • It seems that it doesn't work. Now my client isn't able to connect with server and server side I have this error: Error: Request aborted at IncomingMessage. (C:\Users\Lenovo\Desktop\Universita\Magistrale\ProgrammazioneDistribuita\Progetto\sourcecode\node_modules\formidable\lib\incoming_form.js:122:19) at IncomingMessage.emit (events.js:189:13) at abortIncoming (_http_server.js:449:9) at socketOnClose (_http_server.js:442:3) at Socket.emit (events.js:194:15) at TCP._handle.close (net.js:597:12) – Giorgio Di Rocco Jun 20 '19 at 15:18
  • Try using enctype="multipart/form-data" in html like
    . Let me know if any errors.
    – Darshan Devrai Jun 20 '19 at 15:34
  • enctype="multipart/form-data" doesn't fix the previous error – Giorgio Di Rocco Jun 20 '19 at 15:38
  • If this does not work then I recommend you to user axios library in client application. Sending form data in axios is pretty straight forward. Look at this question - https://stackoverflow.com/questions/47630163/axios-post-request-to-send-form-data – Darshan Devrai Jun 20 '19 at 15:38
  • I solve using this schema: const formidable = require('formidable'); app.post('/sendFiles', function(req,res) { new formidable.IncomingForm().parse(req, (err, fields, files) => { if (err) { console.error('Error', err) throw err } console.log('Files', files); }) res.end(); }); I follow this link https://flaviocopes.com/express-forms-files/ – Giorgio Di Rocco Jun 20 '19 at 15:44
0

I think you need some middleware to accept the multipart formdata in the server side. Multer is a good option.

You can use

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

and then update your server side to handle the upload:

app.post('/sendFiles', upload.array('files', maxCount), function(req,res)
a94
  • 564
  • 1
  • 7
  • 16