0

I want to upload files from client to server and then store them in a folder corresponding to the user that uploaded that particulart file! The code seems to be ok, but the problem seems to be fs-related! Fs only transfers 15 bytes of the file. In fact it only transfers 15 bytes of every file type I have tried so far (images and videos)! This is my code so far, can you please help me? Thank you!

var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();


app.post('/upload', multipartMiddleware, function(req, res){
  var file = req.files.thumbnail; // that's what I've named the html input element
  var stream = fs.createWriteStream(file.path);
  fs.writeFile('./users/'+req.user._id+'/'+file.originalFilename, stream);
  res.redirect('/');
});
Jim
  • 131
  • 1
  • 3
  • 12
  • I think you should add a callback to the `writeFile` call and only redirect after the write was complete, to a success or error page. – XCS Apr 26 '16 at 11:27
  • Tried it! Same thing, only 15 bytes get transfered! – Jim Apr 26 '16 at 11:32
  • And if you log the `req.files.thumbnail` contents in the console do you get all data? – XCS Apr 26 '16 at 11:33
  • Also, try like this: http://stackoverflow.com/questions/2496710/writing-files-in-node-js – XCS Apr 26 '16 at 11:34
  • If I log it I get: { fieldName: 'thumbnail', originalFilename: '(name of the file)', path: 'C:\\Users\\Jim\\AppData\\Local\\Temp\\(name of the file)', headers: { 'content-disposition': 'form-data; name="thumbnail"; filename="(name again)"', 'content-type': 'image/jpeg' }, // or video/mp4 or whatever file type size: 412497, name: '(name of the file)', type: 'image/jpeg' } // or video/mp4 or whatever file type – Jim Apr 26 '16 at 11:38
  • With this method: stackoverflow.com/questions/2496710/writing-files-in-node-js 53 bytes got transfered, but still not the whole image. Anything else? Thank you! – Jim Apr 26 '16 at 11:45
  • The file content is located in *req.files* – Harpreet Singh Apr 26 '16 at 12:27
  • I still get the undefined file! – Jim Apr 26 '16 at 12:31
  • try *req.files.pipe(fs.createWriteStream('./users/'+req.user._id+'/'+file.originalFilename)* **instead** of fs.writeFile – Harpreet Singh Apr 26 '16 at 12:33
  • I got an error with that: req.files.pipe is not a function – Jim Apr 26 '16 at 12:42

2 Answers2

1

The problem is in these lines:

var stream = fs.createWriteStream(file.path); fs.writeFile('./users/'+req.user._id+'/'+file.originalFilename, stream);

stream, is not the file content of 'file.path'. Instead, it is a stream through which you can write to 'file.path'.

For example:

var stream = fs.createWriteStream(file.path); stream.write('some content'); stream.end(); When you used it to write to a file using fs.writeFile, it's object representation is what is written, which is '[Object,Object]' (15 byes).

What you should be doing is to read from the other file, and hold the content in a String or Buffer, not in a stream:

var stream = fs.readFileSync(file.path); And then write into the destination file. Of course, remember to redirect only on completion, if you are using the async model of file write. There is a writeFileSync() API by the way, through which you can write synchronously as well.

Hope this helps.

Gireesh Punathil
  • 1,344
  • 8
  • 18
0

Try like this:

app.post('/upload', multipartMiddleware, function(req, res){
    var file = req.files.thumbnail; // that's what I've named the html input element
    fs.writeFile('./users/'+req.user._id+'/'+file.originalFilename, file, function(err) {
      if(err) { 
        res.redirect('error_page');
        return console.log(err);
       }

      console.log("The file was saved!");
      res.redirect('/');
  });
});
XCS
  • 27,244
  • 26
  • 101
  • 151
  • The file gets saved, but it's still 15 bytes! – Jim Apr 26 '16 at 12:07
  • What if you replace the `var file = thumbnail` part with `var file = "1234567891011121314151617181920"`, what gets written? – XCS Apr 26 '16 at 12:12
  • A file gets created to the desired directory but it's named undefined, and it's not a photo/video file! – Jim Apr 26 '16 at 12:16
  • Yes I know, you should also replace the file name (the first parameter of the `writeFile` function) and see if it writes the entire string. – XCS Apr 26 '16 at 13:02
  • In that case, I still get the undefined file, it just has the name I gave it in the function. Anything else? – Jim Apr 26 '16 at 13:14
  • What do you mean by undefined file? It's type is undefined as long as you don't add an extensjon, but you should still be able to see it's text content – XCS Apr 26 '16 at 16:55