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
Post a Comment