Home > Back-end >  file preview without downloading it using express
file preview without downloading it using express

Time:07-19

I'm trying to develop a file management web app and I want to add an option to preview files before downloading it I'm using Node Js express, is there any way to implement that.
or just a small guide because I tried all the possible solutions on the net but it's not working

CodePudding user response:

Yes you can preview it, you just need to add declaration for your file path and you can access directly from url

const app = express();
const __dirname = path.resolve()
//public is the name of folder where your file is being stored
app.use("preview",express.static(path.join(__dirname,"public")));

So you can directly access the file using url expressurl/preview/filename and preview it wherever you want

CodePudding user response:

You can use the built in node filesystem module fs to read and write to files on the server. So in your case you want to read the content of files, so something like this:

var fs = require('fs');

fs.readFile('my-file.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data);
});
  • Related