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.
Related
I want to do following:
User creates a request using a form-data
The form-data contains and multipart files(upload them via multer)
I want to save logs about files and params. Can you help how can i do this, only if files provided
Thanks
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/
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);
I am new to nodejs and I am trying to call an api server with url parameters stored in a text file.
The text file is a large one (around 20 gb). Now how can I read that file and make urls based on the parameters and call the api server?
The api server doesn't return much information. It just returns OK or nothing. So, the response doesn't matter now. It can be a 'fire and forget' request. If we can handle response, it will be good so that I can re run failed requests (if any) based on response code or response text
Thanks
You want to look into FileSystem.createReadStream http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options
What I have:
I'm using node.js with the express framework and I'm building a form where I can upload files, I'm using node-formidable for this.
What's the problem
My problem is that I can't close the request if I found an error with the uploaded files. I want to check file types, size etc.. So I can get the proper files uploaded, and the files are actually not uploaded so I don't waste time.
So the problem is that I can't stop the HTTP request.
What I have tried
Here is what I have tried so far:
request.connection.destroy();
response.end('something went wrong...');
I assume that the connection.destroy() aborts the request, and I know this because it fires the formidables form abort event (form.on('abort', function(){ ... })) But the file is still uploading, and the response doesn't arrives just after the file was uploaded.
So how should I close the HTTP Request, and send a message back to the client?
EDIT:
And something else, when I don't use response.end() then it works, except that the Client waits for the the answer, it's weird :\
The correct way for doing this is:
request.pause();
response.status = 400;
response.end('something went wrong...');
Source: https://groups.google.com/forum/?fromgroups=#!topic/nodejs/2gIsWPj43lI
Try with following code:
When your server side validation fails, give a response like:
res.json({'IsFileUploaded':'false', 'requiredFileSize': '1000bytes', 'actualFileSize': '800bytes', 'AlertMsg':'File size should be at least 1000 bytes....'})
It closes connection as well as you can pass your data in JSON format