5

I`m using "xlsx" module on serverside. My server's gettings .xlsx files from client. It works great when file is uploaded with multipart. But when i'm trying to download files from Google Drive or Dropbox, i alway receive corrupted .xlsx files.

Function to download files from Google Drive.

service.files.get({
                auth: auth,
                fileId: fileId,
                alt: 'media'
            }, function (err, response) {
                if (err) {
                    res.status(400).json({message: "Error while downloading"});
                } else {                        
                    fs.writeFileSync(req.body.fileName, response);
                    var data = xlsParser.parse(fs.readFileSync(req.body.fileName));
                    res.json(data);
                }
            });

Parser code

module.exports = {
parse: function (file) {
    var workSheet = xlsx.read(file, {});
    return Object.keys(workSheet.Sheets).map(function(name) {
        var sheet = workSheet.Sheets[name];
        return {name, data: xlsx.utils.sheet_to_json(sheet, {raw: false})}
    })
}}

Files on Google Drive are valid. After writing them with node I can't open them. Error i get

\node_modules\jszip\lib\dataReader.js:25
        throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?");
        ^

Error: End of data reached (data length = 1771452, asked index = 1771464). Corrupted zip ?
Geek Guy
  • 710
  • 1
  • 8
  • 28
  • Can you rename the `.xlsx` file to `.zip` and open it with your archieve program? If it doesnt work, try to open it with a text editor and look for a string that shouldn't be there in the first or last line. Like a warning from node that gets written into the file by accident. – Daniel W. Nov 02 '16 at 11:25
  • Cant open it with archieve program and there's no stings like that – Vitaliy Rokossovyk Nov 02 '16 at 11:34
  • did you find solution? having the same problem with exceljs library while reading from file – Geek Guy Mar 22 '19 at 13:36
  • my case: xlsx file gets corrupted for some reason. Restoring it to a previous healthy version worked for me. – Ozgun Senyuva Feb 06 '22 at 17:35

1 Answers1

1

While using xlsx and exceljs modules, both the writeFileSync and readFileSync must be promises and you should wait for them to resolve before performing the next operation.

Gaurav
  • 11
  • 2