MY

ajax 엑셀 다운

hamaganatanadda 2023. 5. 13. 17:20
function ajax2(){
    	$.ajax({
    	    url: "/makeExcel",
    	    type : 'get',
    	    async : false,
            cache: false,
            xhrFields: {
                responseType: "blob",
            },
    	    beforeSend : function() {  //ajax 호출전 progress 초기화
    	        $.LoadingOverlay("show");
    	    },
    	    success : function(blob, status, xhr) {
                var fileName = "";
                var disposition = xhr.getResponseHeader("Content-Disposition");

                if (disposition && disposition.indexOf("attachment") !== -1) {
                    var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                    var matches = filenameRegex.exec(disposition);

                    if (matches != null && matches[1]) {
                        fileName = decodeURI(matches[1].replace(/['"]/g, ""));
                    }
                }

                // for IE
                if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveOrOpenBlob(blob, fileName);
                } else {
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);

                    console.log("ajaxBlob2 fileName: " + fileName)
                    if (fileName) {
                        var a = document.createElement("a");

                        // for safari
                        if (a.download === undefined) {
                            window.location.href = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = fileName;
                            document.body.appendChild(a);
                            a.click();
                        }
                    } else {
                        window.location.href = downloadUrl;
                    }
                }
                

                setTimeout(function() {
                    document.body.removeChild(a);
                    window.URL.revokeObjectURL(downloadUrl);
                }, 100);
    	    },
            error: function(xhr, status, error) {
                alert("An error occurred while downloading the Excel file: " + error);
            },
    	    complete : function() {
                $.LoadingOverlay("hide");
    	    }
    	  });
    }
    
    
    
    function ajax3(){

        $.LoadingOverlay("show");
    	$.ajax({
    		//ajaxzzzzzzzz
            url: "/makeExcel",
    	    data: {"id":"홍길동", "pw":"password"},
    	    type: 'GET',
    	    async : false,
    	    cache: false,
    	    xhrFields: {
    	        responseType: "blob",
    	    },
    	})
   	    .done(function (blob, status, xhr) {
   	    	console.log("ajaxBlob2 result")
   	        // check for a filename
   	        var fileName = "";
   	        var disposition = xhr.getResponseHeader("Content-Disposition");

   	        console.log(disposition);
   	        if (disposition && disposition.indexOf("attachment") !== -1) {
   	            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
   	            var matches = filenameRegex.exec(disposition);

   	            if (matches != null && matches[1]) {
   	                fileName = decodeURI(matches[1].replace(/['"]/g, ""));
   	            }
   	        }

   	        // for IE
   	        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
   	            window.navigator.msSaveOrOpenBlob(blob, fileName);
   	        } else {
   	            var URL = window.URL || window.webkitURL;
   	            var downloadUrl = URL.createObjectURL(blob);

   	            console.log("ajaxBlob2 fileName: " + fileName)
                console.log("ajaxBlob2 url: " + downloadUrl)
   	            if (fileName) {
   	                var a = document.createElement("a");
   	                console.log("a:  " + a.download);

   	                // for safari
   	                if (a.download === undefined) {
   	                    window.location.href = downloadUrl;
   	                    console.log("undefined");
   	                } else {
   	                    a.href = downloadUrl;
   	                    a.download = fileName;
   	                    document.body.appendChild(a);
   	                    a.click();
   	                }
   	            } else {
   	                window.location.href = downloadUrl;
   	            }
   	        }
            setTimeout(function() {
                document.body.removeChild(a);
                window.URL.revokeObjectURL(downloadUrl);
            }, 100);
   	    })
		  .always(function() {
		    console.log("always");
		    $.LoadingOverlay("hide");
		  })
		  .fail(function() {
	          console.log("fail");
		  });  	
    }

https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post

'MY' 카테고리의 다른 글

참고용 링크  (0) 2023.09.10
thread processing  (0) 2023.08.21
code style  (0) 2023.08.05
Substring  (0) 2023.07.22
내가 보는 잡다한 기록  (0) 2023.04.29