javascript - Corrupted download in AngularJs app -
i trying download file using filesaver.js, corrupted file whenever hit download button.
app backed php rest service, , using curl command line confirms rest working ok.
here last version of pseudo-code use downloading:
// let str data received $http promise // code run in "then" callback var arr= new uint8array(str.length); for(var i=0; i<str.length; i++) { arr[b]=str.charcodeat(i); }; var blob = new blob([arr], {type: 'application/octet-stream'}); saveas(blob, "afilename");
it corrupts file.
i tried without applying uint8array
, , giving str
directly blob
. guessed, failed too.
i writing server , client myself, can change in them. thing want handle downloads client-side, , can't redirect users file url download it, because rest needs authentication , token lives within angular app. not choice change server handle file downloads without authentication.
the rest has urls /files/:id
when accessed, gives file content regular http file download (tested curl said above). $http
problem? somehow forcing kind of encoding or assumptions on received data. don't have experience in angular, anyway.
i have found problem. me :)
i had specify responsetype: "arraybuffer"
$http
give str
directly blob
.
fixed code
// let str data received $http promise // code run in "then" callback // make sure specify "responsetype: "arraybuffer"" $http var blob = new blob([str], {type: 'application/octet-stream'}); saveas(blob, "afilename");
i changing responsetype
in wrong place.
Comments
Post a Comment