Follow these steps to download a Word document from .NET Web API to ajax.
- Convert a file to base64 format (this is just 2 lines of code in C#)
- Return base64 string to front end.
Convert base64 string to blob using below function.
base64toBlob = function (base64Data, contentType) {
contentType = contentType || '';
var sliceSize = 1024;
var byteCharacters = atob(base64Data);
//var byteCharacters = decodeURIComponent(escape(window.atob(base64Data)))
var bytesLength = byteCharacters.length;
var slicesCount = Math.ceil(bytesLength / sliceSize);
var byteArrays = new Array(slicesCount);
for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
var begin = sliceIndex * sliceSize;
var end = Math.min(begin + sliceSize, bytesLength);
var bytes = new Array(end - begin);
for (var offset = begin, i = 0 ; offset < end; ++i, ++offset) {
bytes[i] = byteCharacters[offset].charCodeAt(0);
}
byteArrays[sliceIndex] = new Uint8Array(bytes);
}
return new Blob(byteArrays, { type: contentType });
}
Render blob as a file. This has to be handled slightly differently in Chrome vs IE. For Chrome, we need to use link download option.
Please refer to this link for IE 11:
https://msdn.microsoft.com/en-us/library/hh779016(v=vs.85).aspx