admin 管理员组

文章数量: 887017

最近做了一个blob数据类型的pdf文档直接在浏览器打开,有一些难度。
首先需要给后台发送一个{responseType:‘blob’},让后台返回的数据类型为blob
代码如下:

var promise = model.postData('path',params,{responseType:'blob'});
	promise.then(function(resp){
		var blob = resp.data;
		if(window.navigator && window.navigator.msSaveOrOpenBlob){
			var ieBlob = new Blob([resp.data],{type:'application/pdf'});
			window.navigator.msSaveOrOpenBlob(ieBlob);
		}else{
		    if(/constructor/i.test(window.HTMLElement) || (function(p){return p.toString() === '[object SafariRemoteNotification]';}) (!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification))){
				  var safariBlob = new Blob([blob],{type:'application/pdf'});
				  window.saveAs(safariBlob,stmtCde);
			  }else{
					 var link = window.URL.createObjectURL(blob);
					 	window.open(link,'','width = 700,height = 400');
				}
		 }
	})

本文标签: 浏览器 文档 JS pdf