Hello @everyone
I have asp.net core mvc application in which using jQuery ajax
I am allowing user to view and download pdf files generated from byte array.
My code works perfectly fine in Chrome and FF but when in Internet explorer both download and preview is not working.
Please help me out below is my code any suggestions will be appritiated.
// For preview JS
function GetDocPreview(fileLeafRef, ID, SitePath, FileUrl) {
var urlPrv = $('#urlPrv').data('request-url');
$.ajax({
type: "POST",
url: urlPrv, // do not hard code your url's
data: {
'fileLeafRef': fileLeafRef, 'ID': ID, 'SitePath': SitePath, 'FileUrl': FileUrl, 'DeptSiteColID': $("#DeptSiteColID").val(), 'staffID': $("#hdnStaffID").val()
},
success: function (data, jqXHR, response) {
if (data.success) {
var bytes = _base64ToArrayBuffer(data.message);
// saveByteArray("Sample Report", bytes);
var getFile = new Blob([bytes], { type: data.type });
var fileURL = URL.createObjectURL(getFile);
$("#embedPreview").attr('src', fileURL);
}
}
});
}
//For download files
$(document).on('click', '#download', function () {
var ID = $(this).attr('file-id');
var filename = this.innerHTML;
var urlPath = this.attributes[2].nodeValue;
if (urlPath == '#') {
urlPath = this.attributes[3].nodeValue;
}
var urlPrv = $('#urldownload').data('request-url');
$.ajax({
type: "POST",
url: urlPrv, // do not hard code your url's
data: { 'fileLeafRef': filename, 'ID': ID, 'SitePath': urlPath, 'FileUrl': urlPath, 'DeptSiteColID': $("#DeptSiteColID").val(), 'staffID': $("#hdnStaffID").val() },
success: function (data, jqXHR, response) {
if (jqXHR == "success") {
var bytes = _base64ToArrayBuffer(data.message);
saveByteArray(filename, bytes);
var blob = new Blob([bytes], { type: data.type });
var fileURL = URL.createObjectURL(blob);
}
}
});
});
function saveByteArray(reportName, byte) {
var blob = new Blob([byte], { type: "application/pdf" });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = reportName;
link.download = fileName;
link.click();
};
//Base64 conversion
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}