在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 在vue里下載pdf文件打開文件后空白的問題

在vue里下載pdf文件打開文件后空白的問題

1.在做一個pdf導(dǎo)出功能的時候卡主了,是可以下載的,但是下載后的pdf文件打開是空白的,用postman測試下載后臺的pdf文件是可以正常打開并且有內(nèi)容的

2.下載代碼:

downLoadResume(){
    axios.post('url',{
        responseType: 'arraybuffer'
    }).then((res)=>{
        console.log(res);
        if(res.status == 200){
            let blob = new Blob([res.data], {
                type: `application/pdf;charset-UTF-8`//word文檔為msword,pdf文檔為pdf
            });
            let objectUrl = URL.createObjectURL(blob);
            console.log(objectUrl);
            let downEle = document.createElement("a");
            let fname = `download`; //下載文件的名字
            downEle.href = objectUrl;
            downEle.setAttribute("download", fname);
            document.body.appendChild(downEle);
            downEle.click();
        }
        // fileDownload(res.data,'resume.pdf')
    }).catch((err)=>{
        console.log(err);
    })
}

圖片描述

回答
編輯回答
夕顏

為什么不通過a標(biāo)簽直接下載 而要用axios轉(zhuǎn)一圈呢

2017年9月17日 01:48
編輯回答
巷尾

為方便我用fetch試了下,一點(diǎn)問題都沒有。你把你的文件做個md5和源文件對比下是不是一樣,你怕只是單純的打不開pdf文件吧。

fetch('http://60.10.25.240/api/common/UEditorDownload?suburl=ueditor/upload/file/20180802/1533197094431007371.pdf').then(function(response) {
    if (response.ok) {
        return response.arrayBuffer();
    }
    throw new Error('Network response was not ok.');
}).then(function(arraybuffer) {
    let blob = new Blob([arraybuffer], {
        type: `application/pdf;charset-UTF-8` //word文檔為msword,pdf文檔為pdf
    });

    let objectURL = URL.createObjectURL(blob);

    let downEle = document.createElement("a");
    let fname = `download`; //下載文件的名字
    downEle.href = objectURL;
    downEle.setAttribute("download", fname);
    document.body.appendChild(downEle);
    downEle.click();
}).catch(function(error) {
    console.log('There has been a problem with your fetch operation: ', error.message);
});
2017年8月14日 00:13