5735

Proper way to use connect-multiparty with express.js?

I am trying to upload files to my server and extract them from the post request using the connect-multiparty middleware. However, when I receive the request on the server, the req.files and req.body objects are empty (not null, but node-inspector shows that they are Objects with nothing in them.

Here is the code that I'm working with:

server.js:

var express = require( "express" ); var app = express(); var server = require( "http" ).Server( app ); var fs = require( "fs" ); var multipart = require('connect-multiparty'); app.use( express.static( "public" ) ); app.use( multipart() ); app.post( "/httpUpload", function( req, res ) { console.log( "Received post request" ); }

index.html:

<form action="/httpUpload" method="post" enctype="multipart/form-data"> <input type="file" id="uploadFileInput"> <div class="row"> <div class="col-md-6"> <input type="submit"> </div> </div> </form>

I've gotten similar results trying to use multer, connect-busboy, and body-parser. I would have loved if this solution worked for me, but it didn't: http://howtonode.org/really-simple-file-uploads

So ... the only common theme in all of my failed attempts is me. ;o) Any ideas what I'm doing wrong?

Answer1:

Well ... this isn't quite an answer to my question, but when I changed my code to remove the form and send the post request via jQuery's ajax method, the req.files object had my data in it. (shrug) Here was the code in my js that made it work:

$.ajax( { url: "/httpUpload", type: "POST", processData: false, // important contentType: false, // important dataType : "json", data: formData } );

This is the post that put me on the right path: File Upload without Form

Answer2:

here is my way of extracting the uploaded files :

var express = require('express'); var multiparty = require('connect-multiparty'), multipartyMiddleware = multiparty({ uploadDir: './imagesPath' }); var router = express.Router(); router.post('/', multipartyMiddleware, function(req, res) { console.log(req.body, req.files); var file = req.files.file; console.log(file.name); console.log(file.type); res.status(200).send('OK'); }); module.exports = router;

this will save you uploded files to imagesPath folder

Recommend

  • PHP - sending email with attachment does not show message content
  • Error “Reflect.defineMetadata” while trying to load a transient web worker
  • JSON Error when parsing “… has no method 'replace'”
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Form tag not showing up in haml file
  • Pass Dictionary to Javascript array
  • Button text different than value submitted in query string
  • Odoo 10 - Overriding unlink method
  • How to handle images sent by a mobile device?
  • Detect when Facebook like button is clicked
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Meteor helpers not available in Angular template
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • req.body is undefined - nodejs
  • Volusion's generic SQL folder, functionality
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Updating server-side rendering client-side
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Microsoft Visual Studio Community 2015 always crashes in Windows 10 if swithed to Visual FoxPro
  • Upload files with Ajax and Jquery
  • jquery mobile loadPage not working
  • jQuery tmpl and DataLink beta
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to pass list parameters for each object using Spring MVC?
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • How to set the response of a form post action to a iframe source?
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal