0

gridfs-stream upload a file not working, but download a file working plz give solution

app.post('/file', function(req, res) {
var busboy = new Busboy({
    headers: req.headers
});
var fileId = new mongo.ObjectID();
var file = filename();
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    if (!file) return res.send({
        result: 'NO_FILE_UPLOADED This Error by not upload any file'
    });
    console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
    var writeStream = gfs.createWriteStream({
        _id: fileId,
        filename: my_file.txt,
        mode: 'w',
        chunkSize: 1024,
        root: fs,
        content_type: mimetype
    });

// here how can i give a path to read stream ?
// File id is a mongodbid its ok. but what about file name

fs.createReadStream('/some/path').pipe(writestream);
}).on('finish', function() {
    res.writeHead(200, {
        'content-type': 'text/html'
    });
    res.end('<a href="http://localhost:49106/file/' + fileId.toString() + '">download file</a>');
});
req.pipe(busboy);//its working
});

gridfs-stream file upload code is not working file download code is working very well.how can i set the path of the readstream files in the path.

michelem
  • 14,430
  • 5
  • 50
  • 66

1 Answers1

0

busboy never directly writes to disk. What you get is just a plain readable stream (file), so pipe that to your writeStream:

file.pipe(writeStream);

instead of:

fs.createReadStream('/some/path').pipe(writestream);

Also your if (!file) check will never execute, because file will always be defined, and console.log('File [' + fieldname + '] got ' + data.length + ' bytes'); is wrong because there is no data variable in that scope.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • 1
    even thou "file.pipe(writeStream); files not uploading into writestream..but if i add a file into mongodb using robomongo ..and give video id from that db.....its working....so only readStream working in this code.....write Stream not working....plz help me – DAVID RAJU Aug 02 '15 at 21:33
  • Its Complete Code of my server.js please solve the writestream Problem – DAVID RAJU Aug 02 '15 at 21:34
  • You're going to have to elaborate some more. "not working" doesn't tell me anything. Also, there is nothing wrong with the code in the gist you linked to. – mscdex Aug 02 '15 at 23:07
  • I am also using the pips in the same way as you stated in your answer but the file upload size is always 0. I am really not sure what the problem is. – Yogeshwar Singh Mar 31 '20 at 09:08