javascript - node.js upload and download pdf file -


framework: node.js/express.js/busboy/gridfs-stream(mongodb)

i using busboy upload files , use gridfs-stream store files in mongodb gridfs.

                req.pipe(req.busboy);                 req.busboy.on('file', function (bus_fieldname, bus_file, bus_filename) {                  var writestream = gfs.createwritestream({                     filename: bus_filename,                 });                  bus_file.pipe(writestream);                  writestream.on('close', function (file) {                     res.redirect('/xxxxx/');                 });             }); 

download simple: use gridfs-stream's createreadstream read contents mongodb , use following code send browser.

            gfs.findone({_id: attachmentid}, function (err, file) {             if (err || !file){                 res.send(404);             }else{                 var filename = file.filename;                 var readstream = gfs.createreadstream({_id: attachmentid});                 var buffer = "";                 readstream.on("data", function (chunk) {                     buffer += chunk;                 });                  // dump contents buffer                 readstream.on("end", function () {                     res.set("content-disposition","attachment; filename=" + filename);                     res.send(buffer);                 });              } 

problem: when upload 90kb pdf file, uploads fine. see size correct in mongodb. when download, file size of downloaded file 165kb. there mismatch. not happen text files. sure data type.

can please help?

pipe gfs read stream response directly. works me

res.set("content-disposition","attachment; filename=" + filename); var readstream = gfs.createreadstream({_id: attachmentid}); readstream.pipe(res); 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -