ajax请求返回数据转换为Blob对象进行下载文件

 

toExport: function(urlStr,name) {
			var _this = this;
			var xhr = new XMLHttpRequest()
			var fileName = name + '.xls'; // 文件名称 
			xhr.open('GET', urlStr, true);
			xhr.responseType = 'blob';
			xhr.setRequestHeader("Authorization","Basic a2VybWl0Omtlcm1pdA=="); // 请求头中的验证信息等(如果有)
			xhr.onload = function(res) {
				if (this.status === 200) {
				  	var type = xhr.getResponseHeader('Content-Type');
				  	var blob = new Blob([this.response], {type: type});
				  	if (typeof window.navigator.msSaveBlob !== 'undefined') {
					    /*
					     * For IE
					     * >=IE10 
					     */
				    	window.navigator.msSaveBlob(blob, fileName);
				  	} else {
				  		/*
				  		 * For Non-IE (chrome, firefox) 
				  		 */
					    var URL = window.URL || window.webkitURL;
					    var objectU

你可能感兴趣的:(authorization,Blob)