wx.downloadFile({ // 下载文件
url: pdfurl,
success: function (res) {
let filePath = res.tempFilePath // 文件临时路径
wx.openDocument({ // 预览文件
filePath: filePath,
fileType:'pdf',
success: function (res) {
console.log(res);
},
fail: function (error) {
console.log(error);
}
})
},
fail: function (error) {
console.log(error);
}
})
按照上面的代码,一般是没什么问题的,但还是遇到了坑。pdf格式的文件,传给downloadFile,返回来却是html格式,导致文件打开空白。这时看一下获取文件的响应头是什么类型的,application/pdf类型才是正确的!错误的类型就要找后端解决啦!