2

I've pawed through numerous (and mostly outdated) older stack overflow and Google answers and can't find a damn thing that works with the latest versions of Node and Express.

What is the current go-to plugin for async file uploads?

EDIT: I'm uploading the files to my Node.js server. It's running Express. It should be able to handle any file types.

1
  • Upload to what? It depends entirely upon what your sending the file to and what kind of uploads it supports. Commented Sep 16, 2014 at 4:08

1 Answer 1

2

I use formidable for file uploads. You can either store them inside of a directory or you can use Amazon S3 to store them on their servers. It works like a charm.

Here is what some code looks like:

  // At the top of your modules
  var formidable = require('formidable');

  var form = new formidable.IncomingForm(); //Receive form

  form.parse(req, function(err, fields, files) { //Parse form and data
     // Do form stuff, you can access the files
  });

With jQuery, you can do the following:

     $('#your_form').on('submit', function(e) {
        var formData = new FormData($(this)[0]);
        $.ajax({
            url : '/your/url/',
            type: 'POST',
            contentType: 'multipart/form-data',
            data: formData,
            success: function (msg) {
               console.log(msg);
            },
            processData: false
        });

        e.preventDefault();
    });
Sign up to request clarification or add additional context in comments.

10 Comments

Is this for async uploads? (ie. the page doesn't refresh) If so, could you show me the front-end html/javascript?
@opticon, yes. There is a way to do it. jQuery can do that, and I've used it as well.
@opticon I've updated my code. This should work for you. Good luck.
So far it's looking good! Care to give me a peak at whats within the form.parse callback function?
@opticon I recommend you use AWS s3 to store your images. It's what I use. I have the code to do that, however if you're going to store them into your local directories, you'll have to use a built-in module called "filesystem" (var fs = require('fs');) - which one do you want?
|

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.