Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save on client side file corrupted #33

Open
sunojvijayan opened this issue Jan 5, 2018 · 5 comments
Open

Save on client side file corrupted #33

sunojvijayan opened this issue Jan 5, 2018 · 5 comments

Comments

@sunojvijayan
Copy link

From server I am using this to send the file to client.

response.xls('data.xlsx', jsonArr, 'binary');

In client I am using this to download file.

var blob = new Blob([result.data], { //type: 'application/vnd.ms-excel' type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' //type: 'binary' }); var a = document.createElement('a'); var url = URL.createObjectURL(blob); a.href = url; a.download = "sss" //yourfilename a.target = '_blank'; document.body.appendChild(a); a.click();

It downloads the file. But when I open the file, it says corrupt.

Please help.

@PundirKajal
Copy link

@sunojvijayan Hi, I am also facing same issue. If you got any solution please share.
Thanks

@sunojvijayan
Copy link
Author

Hi I was not able to do this in one go. So I saved the file to the server and then gave the link to the client side for the user to download.

@PundirKajal
Copy link

Hi, after struggling a lot, finally i got the solution. I have followed the below approach :

  1. convert json to xlsx using json2xls
  2. write that converted data in file on server.
  3. then read that file and send that data as arrayBuffer to front-end.
  4. on front-end, set response type as arrayBuffer and convert that response to blob and save using filesaver or download by creating url(as mentioned @sunojvijayan )

In my case, 'corrupt file' error was shown while i was sending file data before completion of write file method. To resolve this, i write read file code in callback of writeFile function and send that response to front-end.

Hope this will help.

@ghost
Copy link

ghost commented Nov 27, 2018

Any efficient solution to this?

@ge-hall
Copy link

ge-hall commented Dec 19, 2018

We apply the following preprocessing of the data from json2xls before creating the anchor element as per @sunojvijayan code above.
const decodeBase64 = (str) => {
try {
return window.atob(str);
} catch (e) {
return str;
}
};

const decoded = decodeBase64(data);
const buf = new ArrayBuffer(decoded.length);
const view = new Uint8Array(buf);
for (let i = 0; i !== decoded.length; ++i) {
view[i] = decoded.charCodeAt(i) & 0xFF;
}

const blob = new Blob([buf], {
type: 'application/octet-stream'
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants