0

I'm triying to upload an excel file with postman and save it in to a folder with node.js fs:

var file = req.body.file;

        fs.writeFile("public/myExcelFile.xlsx", file, function(err) {
           if(err) {
               res.json(err);
           } else {
                res.json("The file was saved!");
           }
         });

But when I try to open the file after upload it is corrupt or empty, can you tell me pls what im doing wrong?

In the future I have to upload another files like .docx or .csv so I want to know what is the best way to save these files, it's better save files in database(I´m using Sql server) as a blob ? or save the file in a folder and save the path in database?

  • 2
    What libraries do you use? How is your POST encoded (multipart, urlencoded, ...)? Maybe you should take a look at http://stackoverflow.com/questions/23114374/file-uploading-with-express-4-0-req-files-undefined – Marc Feb 22 '17 at 12:21
  • Thank you @Marc, im using post multipart/form-data, ill try to use connect-multiparty –  Feb 22 '17 at 12:30
  • just to add on in growing discussion, don't save files into project, it'll drastically grow your project size, instead use CDN, cloud storage and only save path in db, and retrieve data from cloud when even needed. Thanks. – Soban Arshad Feb 03 '21 at 10:06

1 Answers1

1

You might want to try using node.js streams. I believe it's best to only save the path in the database.