I am implementing some downloader application. Web client will send some data in web server.
It will process the data and will create a file in some specific format and will push that file to client.
I have done the part till creating the file using nodeJS.
Now can someone suggest me how I can push the file to the client. It is like a downloader application, whenever web client sends the data, using some upload button, it will open a Save As window to save the file in client machine.
So can someone throw some light or pointer on some existing code stuff such that I can have a look?
Thanks in advance.
Regards,
-M-
You can see how do the Express Framework implement that:
https://github.com/visionmedia/express/blob/master/lib/response.js#L356
Line 364: Set the Content-Disposition header to tell the client this response is a attachment that can be downloaded.
this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');
Line 365: Send the file as the body of the response:
return this.sendfile(path, fn);
The send module is used in the above function:
https://github.com/visionmedia/send
Related
I am developing an api based on nestjs. I used multer package to upload file. The code sample on nestjs documentation is at the following:
#Post('upload')
#UseInterceptors(FilesInterceptor('files'))
uploadFile(#UploadedFiles() files: Array<Express.Multer.File>) {
console.log(files);
}
But I want to save uploaded file after send mail. If the mail sends successfully then I will save the file. If sending mail process is fail, I ignore the file uploding.
How can I figure out?
You could do this in two ways, One is to create another route like#Post('mail') then depending on the response you receive in your client, let's say it returns an OK you can send another request to upload the files, or you can send both the requests to your API at the same time then cancel uploading the files (supposing this request takes longer to complete) if sending the mail was not successful (for this you need to handle errors that might result in incomplete files in your API, basically your API should know the full size of the files to expect so that you can compare if there was no cancellation).
The other way is using one route to do both of the tasks, in your example add the code for handling sending the mail and based on the condition that it went successfully do or don't proceed with uploading the files.
I am completely lost here regarding this.
I have a custom API where the endpoint is /api and my client NodeJS script calls towards this endpoint with some form data.
Let's say my client sends a POST with the parameters download_file and file_id,
how would the web server respond with the file data?
I'm sure someone has solved this before but I can't find any information about this.
In the client script where you're going to download the file there are multiple steps you need to do:
create a writestream to a file
make a request to the server
pipe the response stream to the file write stream
The code would look something like the following:
const request = require('request');
const fs = require('fs');
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
I am working on angular (frontend) and node ( backend). I want to send an email using sendgrid , With excel file as attachment. I followed the app as reference demo app .Instead of downloading the excel, I want the same file to be sent as email attachment.
Note : In the backend I use nodejs, I am not sure how to pass the file with API to node.js, I can send email in nodejs code.
Can someone help me on this?
You can make an http post request in the node api back-end and pass the file. Do post using headers as Multipart form request.
Multipart form request helps you to make the node js api pass the excel sheet generated on the front end.
In your email service add the http service call to the backend. And make the service pass the file in it
Check this link out for more details
https://blog.jscrambler.com/implementing-file-upload-using-node-and-angular/
We have a cache of some of the .js files (static assets) on an Express server. We need to send the right headers.
If we do res.send(cache[filepath]), then it will send a response to the browser, but the browser doesn't know it's a file (tmk).
On the other hand, res.sendFile(filepath), takes a filepath, not a string in memory.
So what is the best way to send the file from the cache?
Is it a simple case of:
res.write('\n\n' + cache[filepath] + '\n');
res.end();
?
here is the code I am working on:
https://github.com/ORESoftware/express.fs.cache
here is a relevant conversation on Github:
https://github.com/expressjs/serve-static/issues/107
I'm implementing a webserver using Koa. One of my request handlers needs to receive and store a large zip file. The file is NOT uploaded from a web page, but sent from another NodeJs application. Therefore it's not multipart encoded, but just applcation/octet-stream. How can I read such a file from the request?
I've noticed there is a request.socket object but I could not find any documentation on how to use it.
In other words I need the opposite to
this.body = fs.createReadStream(path);