https://1004lucifer.blogspot.com/2019/10/jquery-ajax-chrome-firefox-ie11.html
function requestDownloadFile(reqObj) {
function getFileName (contentDisposition) {
var fileName = contentDisposition
.split(‘;’)
.filter(function(ele) {
return ele.indexOf(‘filename’) > -1
})
.map(function(ele) {
return ele
.replace(/”/g, ”)
.split(‘=’)[1]
});
return fileName[0] ? fileName[0] : null
}
if (!reqObj || !reqObj.url) {
return;
}
if (!reqObj.data) {
reqObj.data = {}
}
var isGetMethod = reqObj.method && reqObj.method.toUpperCase() === ‘GET’;
return new Promise(function(resolve, reject) {
var option = {
url: reqObj.url,
method: isGetMethod ? ‘GET’ : ‘POST’,
data: $.param(reqObj.data), // a=1&b=2&c=3 방식
// data: JSON.stringify(reqObj.data) // {a:1, b:2, c:3} JSON 방식
xhrFields: {
responseType: ‘text’
}
}
if(getMobileWebview() !== “iOS” || getMobileOs() !== “iOS”) {
option.xhrFields.responseType = ‘arraybuffer’;
}
$.ajax(option).done(function(data, textStatus, jqXhr) {
if (!data) {
reject();
return;
}
if(getMobileWebview() == “iOS” || getMobileOs() == “iOS”) {
window.webkit.messageHandlers.getPdf.postMessage(data);
resolve();
return;
}
try {
var blob = new Blob([data], { type: jqXhr.getResponseHeader(‘content-type’) });
var fileName = getFileName(jqXhr.getResponseHeader(‘content-disposition’));
fileName = decodeURI(fileName);
if (window.navigator.msSaveOrOpenBlob) { // IE 10+
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else { // not IE
var link = document.createElement(‘a’);
var url = window.URL.createObjectURL(blob);
link.href = url;
link.target = ‘_self’;
if (fileName) link.download = fileName;
document.body.append(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
}
resolve()
} catch (e) {
console.error(e)
reject()
}
});
})
}