So I'm attempting to download a file in the server which I get and place in my current directory and when I try to send it back to the user using res.download(file) it seems to send it as plain text instead of sending an attachment to download. I'm currently trying to send a .doc file.
http://expressjs.com/en/api.html#res.download
I have tried to set the headers res.setHeaders('Content-Disposition', 'attachment') but that doesn't seem to work either.
variable file currently has the path to the file exactly.
var file = path.join(__dirname, result)
res.download(file, result, function(err){
if(err){
console.log("err", err)
}
})
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.
I am working on nodeJs(backend) and (Angular)
I want to generate a .ics file URL for google calendar and for Apple and Microsoft as a downloadable file.
I know there is a node module ics and I am using that, but that only creates a .ics file I want this to be unique for each user and also want this to delete automatically.
Also, it should automatically sync with the events added.
any suggestion for this?
I have been unable to find a way that would continually update someone's schedule since this would require continuous access to someone's calendar. While building a scheduling site I got around your storage and unique file problem by using a brute force solution.
First, as a client access the download endpoint an unique ics file is generated and stored as schedule_date_client-name.ics. This unique file is then sent to the user using res.download and promptly deleted using fs.unlink(path_to_file).
Here is an example of this in action :
try {
res.download(path, function(error){
if (error) {
console.log("Error : ", error)
}
fs.unlink(path, (error) => {
// log any error
if (error) {
console.log(error);
}
})
});
} catch (e) {
next(e);
}
The best way I found around this is to generate the .ics file as the user accesses a /download url endpoint. Send the file in a downloadable format using res.download in your controller file. Here is more information on the packages I used for this solution :
Node .ics : https://www.npmjs.com/package/ical-generator
Node file : https://nodejs.dev/learn/the-nodejs-fs-module
Require the modules :
const ical = require('ical-generator');
const fs = require("fs");
I have web application where on button click invoking REST API which fetch list of files available in ftp server and displayed in div with hyperlink on file name as below.
for this I am using jsftp module.
ftp.ls(".", function(err, res) {
res.forEach(function(file) {
console.log(file.name);
//logic to display in div
});
});
I have another REST API for download file from FTP server which invoked on file name click present in div but it is downloaded in local directory where web application deploy and not in users system with below code, but I want when user click on file name that file should download in users system or computer. How I can achieve this.
ftp.get('remote/file.txt', 'local/file.txt', function(hadErr) {
if (hadErr)
console.error('There was an error retrieving the file.');
else
console.log('File copied successfully!');
});
Now that the file is on your machine you will want to actually server the file to the client when they click on the link. Using res.write something like: res.write(file, 'binary');
Here are some concepts that you'll find useful: Node.js send file to client
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.
I am investigating how to download files to a user's local machine but I'm not quite sure what I need in order to do this. I'm using Nodejs and Express with Angularjs on the front-end.
User's can write text into a textarea and it's this text that will be written to the file.
To do this I have:
...
fs = require('fs');
fs.writeFile('filename.txt', textarea.text, function (err) {
if (err) return console.log(err);
res.send(200);
});
...
Once the file is created how do I get it to download on the user's machinea?
Use res.download
res.download('filename.txt');
http://expressjs.com/4x/api.html#res.download
If you don't need to store the file on the server, you could just sent it back to the user directly:
res.attachment('filename.txt');
res.set('Content-Type', 'text/plain');
res.send(textarea.text);
This is not only simpler but also improves performance (no disk i/o) and more secure (no untrusted files on your server).