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.

2 Answers 2

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)
    })
  })
})
Sign up to request clarification or add additional context in comments.

5 Comments

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.<anonymous> (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)
Try using enctype="multipart/form-data" in html like <form id="upload_form" enctype="multipart/form-data">. Let me know if any errors.
enctype="multipart/form-data" doesn't fix the previous error
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 - stackoverflow.com/questions/47630163/…
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 flaviocopes.com/express-forms-files
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.