How to serve word , excel , ppt and pdf files from node.js server - node.js

I have successfully upload file from mongoDB to my node.js server
Screen Short given below:
click here to see Screen Short
but I have no idea how to serve these files from node.js server to client side ..
Very Thankfull to advance

You can use something like this:
With this code, when you click a link to /pdf endpoint, the client will download a file with desired name.
app.get('/pdf', (req, res) => {
res.set('Content-disposition', 'attachment; filename=' + desiredName)
res.type(yourDataType)
res.status(200).send(Buffer.from(yourBufferFromFile, 'binary'))
})

Related

How to create file uploading web service using node js with jwt Authentication?

I want to create file uploading web service with jwt Authentication such that peoples can upload there files to /upload route and I will use cloudinary service in my server to actually store them. User can also get their files using filename with /getfile route. Is there something else I should add in my project?
In order for the express server to be able to take a file as an input I would use the package Express file-upload in order to achieve this. The docs are super simple for this one.
Inside of your server.js file:
// setting temp files location
app.use(fileUpload({
useTempFiles : true,
tempFileDir : '/tmp/'
}));
upload route:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
res.download('/report-12345.pdf')
});
This file obeject caqn then be used to save the file wherever you desire.
For sending the file back to the user Express also has a feature for this which is documented here
getfile route:
app.get('/getfile', function(req, res) {
// whatever file to be downloaded here
res.download('/report-12345.pdf')
});
Hope that's what you were asking for. Thanks!

How to make sure download prompt is triggered using express sendFile?

I have the code below in my express server
app.get('/download', async (req, res)=> {
res.sendFile( `/dl/myfile.mp4`, {headers: {'Content-Type': 'mp4'},root: __dirname})
});
The sent file does not cause a download prompt in any of my tested browsers ( Chrome, Opera ) instead it shows me a player and starts playing the file while I need to it cause a download prompt.
How can I avoid the player and trigger a download prompt using Express?
You shoule use res.download(filePathToServer)
Transfers the file at path as an “attachment”. Typically, browsers will prompt the user for download if you use it.
You can read more about it here res.download - express

Express JS download zip via endpoint

I'm trying to allow a user to be able to download a zip folder from a server via an express JS api call. I've created my folder with: zip -r download.zip folder-to-zip and am struggling to get it to download.
It seems if I make a GET request to some endpoint, it only downloads it to the server rather than the browser, my endpoint is:
router.get('/download', (req, res) => {
res.download('download.zip')
})
I need to attach something to a button that'll download this zip folder.
I haven't tested this, I'll update my answer when I have.
But I think doing something like:
router.get('/download', (req, res) => {
res.setHeader('Content-type','application/zip');
res.sendFile(__dirname + '/download.zip');
})
This is usually how I download files via express.

How to display images in React that are grabbed from an Express server?

I'm just a little confused on how to serve the assets correctly from the source folder.
Relevant server-side code (The client makes a request to /images, the server sends back the file names):
const images = './static/images/';
app.use(express.static('static/images'))
app.get('/images', (req, res) => {
res.setHeader('Content-Type', 'application/json');
fs.readdir(images, (err, files) => {
res.send(files);
});
});
My directory structure looks like this:
public
index.html
server (houses Node/Express code)
index.js
src (houses React code)
App.js
static
images
image1, image2, image4, image4
If I try to display the image in React (state.currImage being one of the 4 file names):
<img alt="scan" src={ require(this.state.currImage)}
I get an error saying "Unhandled Rejection (Error): Cannot find module './static/images/file1.jpg'"
It looks like you could maybe remove the require from the image loading considering it's a path to an image and not a path to a javascript module.
<img alt="scan" src={this.state.currImage}
You may need to update the url of those images to point to the server url. Right now the string is ./static/images/file1.jpg but it may need to be /images/file1.jpg.

How to hide a download path of a file with Nodejs and Express

I have a situation where I want to hide the source path or download path of a file in my server from displaying on html. However, user is still be able to download it.
Any plugin for this? It's build with Nodejs and Express.
Perhaps you should create an API for it? Then you wont give away your file/folder structure.
router.get('/api/download-file', (req, res) => {
res.sendFile(your_file, {headers: {'Content-Type': 'your_file_type'}})
})

Resources