I'm using ASP.NET and C# and services from IIS
I don't know why but everytime i click on "download file" in my PRODUCTION ENVIRONMENT the final PDF has less bytes and it's broken (it should have ended with 22 Kbytes but it downloaded 11Kbytes and that's because the file is broken).
It is strange because in my UAT environment it works just fine so i wonder if there is a problem with the IIS or whatever
I created a function using AJAX to download a file from a given URL (i call an API to get the desired file)
function downLoadFile(url) {
$("#h_msj").text("Downloading file...");
$.ajax({
url: url,
method: 'GET',
esponseType: 'arraybuffer',
success: function (response) {
if (response.fileName.length == 0) {
alert("File not available!!");
}
else {
var arrayBuffer = base64ToArrayBuffer(response.data);
saveByteArray(response.fileName, arrayBuffer);
}
}
});
}).then(function () { $("#h_msj").text(""); });
The URL looks like this:
http://server43.test.local:9867/api/rest/invoices/receipts/pdf?business=01&account=87577&type=TRANSACTION&numberTRX=AGH66
Is anything wrong with my code? should i modify anything? It has something to do with IIS?
I am not an expert on IIS so i don't know if there is something wrong or not but the problem is it works fine on UAT and TEST servers but it fails on the PRODUCTION ENVIRONMENT.
Could you please tell me what to do?
EDIT #1
//convert BASE64 string to Byte{} array
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
//save Byte[] array and download
function saveByteArray(fileName, byte) {
var blob = new Blob([byte], { type: "application/octetstream" });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
link.remove();
window.URL.revokeObjectURL(url);
}
