后台返回文件流,前端实现下载pdf

前端代码:

            var xhr = new XMLHttpRequest();    
	    xhr.open("get", url, true);
	    xhr.responseType = "blob";
	    xhr.onload = function() {
	    	
	        if (this.status == 200) {
	        
	        	var blob = this.response;
	        	if(blob.type == "text/html"){
	        		tip("出错");
	        		return false
	        	}
	    		var fileName = filename;
	    		if(window.navigator.msSaveOrOpenBlob){			// IE浏览器下
	    			navigator.msSaveBlob(blob, fileName);
	    		} else {
	    			var  link = document.createElement("a");
	    			link.href = window.URL.createObjectURL(blob);
	    			link.download = fileName;
	    			link.click();
	    			window.URL.revokeObjectURL(link.href);
	    		}
	       }else{
	    	   iframe.tip("请求错误!");
	       }
	   }
	   xhr.onloadend = function(res){
		   	
	   }
	   xhr.send();

猜你喜欢

转载自blog.csdn.net/huangxinglian/article/details/89207691