正常情况下使用window.open会默认打开预览,出现下载而非预览的情况要先检查响应头的设置;
Content-Disposition是否设置为inline,参数有两个值attchment(表示下载文件)、inline(变表示内嵌显示);设置为inline表示为预览文件而非下载;如果你用不到下载只需要预览可以让后端直接设置inline;如果即需要下载也需要预览;前端可以自行设置为inline;
具体方法我这里是使用axios来实现的,代码如下:
axios({ method: 'get', url: path, responseType: 'blob', headers: { 'Content-Disposition': 'inline' } }) .then(response => { const url = URL.createObjectURL(response.data); window.open(url,'_blank'); }) .catch(error => { console.error(error); });
这里的url为你的地址;
Content-Type:application/pdf;这个参数也要检查是否这样设置;