Isaac Wasserman
Isaac Wasserman

Reputation: 1541

Simple file upload with Node.js

I am building a node.js application that uses pdf.js to read pdf files, but much like other js, pdf.js does not allow cross origin requests. So, I need a way to save files selected with a file input to my pdf directory. I'm not so great with node so make it as simple as possible if you can.

Upvotes: 0

Views: 2222

Answers (1)

Grant Fowler
Grant Fowler

Reputation: 106

Here is a basic idea of what you need:

1st, require and use module 'connect-multiparty'. This will expose the req.files object in node.

var multipart = require('connect-multiparty');
app.use(multiparty({});

Then, in your controller method, require the 'fs' module, and use it to save the uploaded file.

var fs = require('fs');
fs.writeFileSync("myFileName", req.files.file.ws.path, function(err) {
    if(err) { console.log(err); }
    else { console.log("file uploaded"); }
});

Being familiar with node will help, but the two basic libraries you need to perform this are the aforementioned https://www.npmjs.com/package/connect-multiparty and http://nodejs.org/api/fs.html

edit: see the link in the comments below. this answer is incomplete and is better explained in the link

Upvotes: 1

Related Questions