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
saving Zip file problems #156
Comments
solved it with:
usage:
|
I'm having a similar problem with this as outlined here ... http://stackoverflow.com/questions/34756633/trouble-downloading-zip-file-with-spring-boot ... and your fix didn't help me. I think I'm doing something else wrong. Think it might be the way I'm streaming it from Spring but not sure. |
not sure I can help here - in my case I was generating the zip file to be downloaded in the browser, not retrieving it from a server. And I don't do Java. |
My problem turned out to have to do with using angular's $http service to make the call (jQuery's ajax method failed as well). But if I used the builtin XMLHttpRequest it worked just fine! No need to convert the byte array even.
No idea why those other packages returned the wrong data. :( |
@crowmagnumb you might have better luck with the angular $http service in this context by setting responseType to be 'arraybuffer'. I had a similar problem with zip/kmz content from the server until I saw this post: http://stackoverflow.com/questions/26767160/download-a-zip-file-in-angularjs |
@jimgeiger Good to know. I'm not going to go back now to verify as that code is working. But if I have to refactor for some reason I'll definitely give this a try. Cheers! |
Hi, My only problem with it is that I can not distinguish between a file download or an error from the server (i.e. file not found) as everything returns as an arraybuffer that way and there is no way to parse the response. except maybe setting a different HTTP statusCode ... |
am also facing similar kind of issue, I tried giving responseType to be 'arraybuffer' , but didn't work. I could see the zip from which is from spring controller on the ui - IE Network - responsebody is showing the zip, but zip cannot be retrieved in angular |
Here is my code:
|
@crowmagnumb it worked with your suggestion of using XMLHttpRequest |
Seems like a problem with angular. Should I close this issue, or any of you not using angular and experiencing this? |
related to #219 |
@evilaliv3 He's calling |
If anyone finds any errors in FileSaver.js causing this issue, and not angular or the Blob API, please mention me in this issue and I will check it out. |
@divyakumbukkal It seems like the angular $http and $resource modules expect the response to be an Object. In order to make sure that the binary file received is as an arraybuffer, 2 things to be done. I have modified your code and posted a code which I used using $resource service for your reference in case your modified code does not work
Here is my code snippet using the $resource
Let me know if it works for you I found this StackOverflow post helpful for the problem http://stackoverflow.com/questions/35000245/getting-a-binary-file-from-an-angular-resource |
Hi |
use jquery, it worked for me. |
@divyakumbukkal : i tried the solution with XMLHttpRequest but did not work var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = "blob"; // changed this to arraybuffer but did not work as well
xhr.withCredentials = true;
xhr.onreadystatechange = function (){
if (xhr.readyState === 4) {
var blob = xhr.response;
FileSaver.saveAs(blob, filename);
}
};
xhr.send(); |
the same code worked for me, check in developer tools if you are getting zip file properly |
my response header looks like this:
and i can see binaries in the body of the response. the zip file is also properly created locally, i create a local copy on hdd for testing and send stream of it as response data. |
see if you can save zip file inside developer tools of firefox/chrome |
i have "File not found" error in developer tools. |
so its clear that backend was not providing zip properly |
looks like that...but i can still see a zip file saved locally as backup by the server. |
If you have PS, next time you post code block use 3 ` followed by the syntax: eg
but without space |
@eligrey I am having this issue but it's happening to me with AJAX, and axios inside react I have tried changing the enpoint that is generating the zip file and I am still have the problem. I tried with blob with different types, etc. $.ajax({
url: "http://localhost:80",
type: "GET",
responseType: 'arraybuffer',
processData: false,
success: function (res) {
var blob = new Blob([res], { type: 'application/octet-stream' });
var fileName = "QCPReport.zip";
saveAs(blob, fileName);
}
}); |
@ekaitzht jQuery ajax don't support arraybuffer or blob, you creating the blob the wrong way. Read this on how to support it: http://keyangxiang.com/2017/09/01/HTML5-XHR-download-binary-content-as-Blob/ Frankly i think jQuery's ajax sucks. I prefer regular XMLHttpRequest over jQuery's but would definitely choose the fetch api if it's available to you fetch('http://localhost')
.then(res => res.blob())
.then(blob => saveAs(blob, fileName)) or see #156 (comment) for xhr But hey. if the file is coming from the server i would not use the fileSaver lib at all. use content disposition attachment header to tell browser to trigger the download. It has grater cross browser support for all browsers and you won't have to buffer up the clients memory |
[jvsteiner commented on Jul 2, 2015] - This solution worked perfectly for me. The code on my server side looked something like:
|
Hi, just had similar issue using When fetching the zip file, you must ensure that it is not transformed into a string by the client lib, this can corrupt the data. In axios (that rely on |
For those who are using It was helpful in solving my problem. |
@crowmagnumb - Great tip! XHR worked after trying 10-12 different approaches.. Was totally clueless. Thank you! |
Hi Team, I need to save a zip with 3 files of same name. Currently, the zip is storing only 1 file as 3 of the files have same name. Can you please help me here
|
Hi I have been using FileSaver.js with no problems to save binary data generated in browser, but am having problems with the following code:
Note the difference in lengths. The result is that the zip file will not unpack correctly. myData is a string containing binary data. I have also tried reading it into an array first, various different mime types (none, text, octet-stream). Any ideas?
The text was updated successfully, but these errors were encountered: