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);
Related
I want to send to the client in the same request files from the dir and some content from the DB.
DB query -
const derivatives = await Derivative.findAll();
Here is the res -
res.status(200).send({
data: derivatives.map((derivative) => ({
id: derivative.id,
date: derivative.date,
})),
});
Here is the download -
const fileName = derivatives.map((name) => name.wex);
res.status(200).download(__dirname, `/assets/${fileName}`);
How can I add that to my response?
HTTP lets you specify a "content disposition" to indicate whether a response should be treated as a download, but doesn't support sending downloads arbitrarily, it has to be a response to a request. You can't have part of a response be a download and part be not, for a single request.
So if you need a file to be downloaded, and some JSON used to display some UI, you need to handle that in the client somehow. Either:
The client sends a request, and server returns JSON containing a URL for the download as well as the other data you wanted to send, and then the client requests a download of the URL through JavaScript
The client sends two requests, one for the download and one for the other data; this may complicate things on the server if you need to associate the two requests (want to do a database lookup only once for instance), but is simplest on the client.
The client sends a request, and the server returns a response containing the JSON data and the file data, packed in some way (the file data could be inside the JSON but that would be inefficient), and it's unpacked on the client (using JavaScript) and the client then constructs a Blob URL to "download" (in this case the data is already downloaded, so this just entails saving a file)
There are any number of ways you might pack the file and JSON data together, which is what /u/Quentin was alluding to. Sending both as one response may be better for performance, but you probably don't need to.
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 tried to upload an image file to my server. My client is writing with react native and server is node. I use fetch POST and multipart/form-data on the client side, and multer on server side. When I tried to console.log the request, I got a tons of trash information. I followed the example provided online so I have no idea what's going on. Also multer doesn't extract file from request (req.file is undefined). Also I don't really understand if you post local uri instead of base64 data to your server. How does the server know how to retrieve upload the file through local uri.
I am currently working on an application with the current tech stack:
Backend:
Mongoose
Express
Apollo
GraphQL
Frontend:
Vuejs
Apollo
GraphQL
I have succeeded in uploading files to the server using GraphQL, what I am stuck with is how to implement the 'download' feature. With a normal RESTApi endpoint I can use res.download(filePath) and it works. How do I do this with GraphQL since I don't want to use REST.
Or is there any other standard to go by in this scenario?
Thanks!
GraphQL uses JSON format, which represents as text format, not as binary.
If you don't want download files with REST, then you should:
Encode your file content into base64 string in the back end. Related question
Send this string as part of query response.
Save encoded base64 string as a file in the front end. Related question
But right architecture design is add a file link in the GraphQL response and use browser for downloading/rendering the file.
It's better to send a hashed & temporary link to download it
Save the file and hash the name on your static server (to limit access to other users)
The file should be temporary and should expire in a short time
Send the link of the file in response to API
I'm using Heroku to store my Express app and I can't use bodyParser, or formidable, because both uses temp files to store the posted file.
The file I'm posting is simply a large text file (csv). I don't really need to store the file anywhere, because the data will go into mongodb.
Is there anyway to parse the csv file as it is being uploaded?
Starting with Connect 2.9.0 (Express 3.4.0), bodyParser uses multiparty instead of formidable.
In multiparty's default configuration, it does not create temporary files. Read the docs to see exactly how to handle the incoming data, but you'll want to listen to the part event, which gives you a readable stream containing the uploaded file.