0

I want to download byte array into file in Internet Explorer and Microsoft Edge using javascript(c#). Anchor.download element is not supported in these browsers. I have searched a lot and nothing seem to work out. Please help me find a solution.

I have tried Window.location=url. I have tried solution suggested in following: Saving binary data as file using JavaScript from a browser the Blob.js and FileSaver.js is not supported and even that is not working. I need a proper solution where I can download the file with its extension and proper name. I have the data in byte array, I have the name of the file with extension. It is working in Chrome but not in other browsers.

Following is the working code for Chrome :-

var sampleBytes = new Int8Array(responsetext);
var saveByteArray = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, DocName) {
var blob = new Blob(data, { type: "octet/stream" }),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = DocName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
saveByteArray([sampleBytes],DocName);
angel
  • 11
  • 3
  • 2
    it is supported in Edge ... and what the heck is `javascript(c#)`? – Jaromanda X Mar 02 '18 at 08:47
  • 1
    `It is working in Chrome but not in other browsers` - show the working code - perhaps you've made Chrum-specific assumptions – Jaromanda X Mar 02 '18 at 08:48
  • @JaromandaX I wrote javascript(C#) because I am writing this code in JS and getting the array of bytes from C# using Ajax. But that is not my query. Only query is to download the file using JS for all browsers. Attached the working code snippet to download in JS only for chrome. – angel Mar 08 '18 at 11:24
  • 1
    https://github.com/eligrey/FileSaver.js definitely works cross browser in all major browsers - IE, Edge, Chrome, Firefox - I know, I use it every single day – Jaromanda X Mar 08 '18 at 11:48
  • It is not working in IE,Edge because anchor.download is not supported in other browsers except Chrome and FileSaver.js and Blob.js is not getting included into my solution. – angel Mar 08 '18 at 13:35
  • oh, so the problem is that you don't know how to include those two libraries into your solution – Jaromanda X Mar 08 '18 at 20:18
  • Yes, you can say that. I am open to any other working solution as well. I added the reference of both the .js files using drag and drop then also not getting included. – angel Mar 09 '18 at 05:59
  • why can't you include those libraries? the alternative is to write code, that does what those libraries do – Jaromanda X Mar 09 '18 at 07:58
  • Too much of code, also I tried adding a new .js still did not work. I have found another working solution for now, but I also did not got why the .js files were not getting recognized. – angel Mar 29 '18 at 09:55

0 Answers0