I have a usecase where I want to read data from database and send it to frontend as a downloadable zip file. I am stuck on how to achieve this using node js and express.
For now I was trying to send just a downloadable json file and was confused how to achieve it.
What I have tried so far ->
const data = db.read() // fetch an array of objects.
const myBuffer = Buffer.from(JSON.stringify(data)); //creating a buffer
const readableStream = Readable.from(myBuffer);
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', 'attachment; filename=\"my json file.json\"');
readableStream.pipe(res);
trying this from postman gives me json directly. My question is how to create a downloadable zip file of data here and send it to client. I want to make sure I don't use fs to write a file on server and then send it. Any help and guide would be great, Thanks!!
As #ardritkrasniqi suggested to use a third party npm package, I was able to make an in memory zip file and send it to client using npm package archiver.
I created it like this ->
const data = db.read() // fetch an array of objects.
const myBuffer = Buffer.from(JSON.stringify(data)); //creating a buffer
const archive = archiver.create('zip', {});
archive.append(myBuffer, { name: 'test.json'});
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', 'attachment; filename=\"test.zip\"');
archive.pipe(res);
archive.finalize();
Hope this will be helpful for others who are trying to achieve the same.
PS. I am using express as my backend node framework.
Related
I have a client in React that sends a form data with a file. When that file arrives to the server, the body is parsed by body parser and its result is a buffer. The idea is that the file keep saved in some place of my server, because I want to use it later from my client. So I'd like to know how should I handle this problem.
I tried to write directly this buffer as a file with fs, but the file created has an error of format, so I can't access it.
You can do stuff like this
var fs = require('fs');
fs.writeFile('newImage', req.files.image, function (err) {
if (err) throw err;
console.log("It's saved");
});
correct parameter order of fs.writeFile is the filename first then the content.
If you're using express.bodyParser() you'll have the uploaded files in the req.files field.
And of course you should write the callback:
POV: Your image file should be in req.files not the body.
I think you need a package for storing a file on your backend service. I had used morgan package for that and I was satisfied with using it. I have just searched other packages for storing a file, i found express-fileupload package. Maybe you want to look at how to use those. If you want to store a file, using the third package would be better for you and for your effort.
Is there a way to post form-data through postman using Nodejs. I have seen many platforms but the author is using the front-end to post data which manipulates the back-end code. I want to understand the code
Yes you can send the data using form-data in post man.
You can see the formdata under the body section in this form data you have to change the type from text to file
after changing to file type you can browse your local machine. to choose the image
You can further parse the files in node js using formidable. You can install this package and you can configure to your needs. It can handle upto 1000 files
Coming to code part in node js.
First configure the formidable
const form = new formidable.IncomingForm({
multiples: true,
keepExtensions: true,
});
After that you have to parse the fieldvalue and file
form.parse(req);
Then you can seperate the value and files uisng
form.on("file", (field, file) => {}) //For file
form.on("field", (fieldName, fieldValue) => {}) //for fieldvalue
Hope this one helps for you!!
Sure thing. Set request type to POST and then define your data in the request body
check this out
https://learning.postman.com/docs/sending-requests/requests/#sending-body-data
I am new to Express and I need your help.
How to save SVG on server using Express?
const qr = require('qr-image');
const fs = require('fs');
exports.qr = function (req, res) {
var code = qr.image(new Date().toString(), { type: 'svg' });
// I would like to do something like this:
fs.writeFile('qr.svg', code, (er)=> console.log(er));
// and download using express.static
res.type('svg');
code.pipe(res);
};
Currently I am returning image as a stream and it works fine.
I have an api with mongodb built on Express and I would like to store QR Codes on the server side. Api is for the application built with Xamarin managing event tickets.
QR images are going to be downloaded more than once, that's why I would like to put them on server.
Maybe better way would be to store them with SQLite locally on the client device? Or maybe I should just send json data to be parsed into SVG?
How do you think?
At the moment I am not implementing db locally.
After some time when I went back to the topic I found the solution.
You need to add new action to your pipe. Simple as that:
var code = qr.image(ticket.code, { type: 'svg' });
code.pipe(fs.createWriteStream('i_love_qr.svg'));
I'm working with NodeJS and using HAPI to create API for upload and download file. When uploading, I read the file information (filename, mime type and file content) and store it in database. File content is stored as base64 encoded string.
What I want to do is to create API, so when client hits it will be forced to download a file that is constructed based on the stored information using the code below
server.route({
method: 'GET',
path:'/file',
handler: function (request, reply) {
var fileData = // get file content;
var mime = // get file mime-type;
var fileBuffer = new Buffer(fileData, 'base64');
reply(fileBuffer)
.header('Content-disposition', 'attachment; filename=' + fileName)
.header('Content-type', mime).header('Content-length', fileBuffer.length).encoding('binary');
}
})
But this code looks like still not work, if I hit the API it will be loading process forever and no file downloaded. Anybody can give me suggestion on how to do it correctly?
UPDATE
the code is correct and works perfectly. the problem I had before is caused by another reason, incorrect encoding/decoding mechanism.
Check out the inert plugin for hapi which handles files, the repo is here
I'm trying to generate a zip file in memory on the server and sending it to the client as a download file.
Basically, there's a html page where the client types what file he wants.
The server receives what the client typed (via socket.io) and does a search on a mongodb. At this point, the server returns a link like this one
<a href='#' onclick='socket.emit("generateFile", id, path); return false;'>link</a>
Where id is the id of the entry in the database and path is the location of one of the files that will be in the zip.
After that, that server is supposed the create a json containing the entry that has that id and zip it along with some local files available on the server. My question is: how do I send this generated zip to the client? Remember: I want to send it without having to save it in the server's hdd. I tried using Express after the zip is created but the code doesn't reach this piece:
app.get('/', function(res, req)
{
res.set('Content-Type', 'application/zip')
res.send(generatedZipFile)
})
How to proceed?
This is from express-static-zip implementation
var contentType = mime.lookup(name);
if (contentType != 'application/octet-stream') {
res.set('Content-type', contentType);
var charSet = mime.charsets.lookup(contentType);
if (charSet) {
entryData = entryData.toString(charSet);
}
}
res.status(200).send(entryData);
You might want to set status code and mime format.