uniapp使用uni.downloadFile下载客户端传过来的文件地址,我是.docx,为什么下载之后变成了.pdf ?-灵析社区

栀子花爷爷

uniapp使用uni.downloadFile下载客户端传过来的文件地址,我是.docx,但是下载之后变成了.pdf export const saveFile=(url,open=true)=>{ url = encodeURI(url) console.log('ccc', url) // return return new Promise((resolve,reject)=>{ uni.downloadFile({ url, // success: function (res) { console.log(res) uni.saveFile({ tempFilePath:res.tempFilePath, success: function (res) { console.log('保存成功') console.log(res) let {savedFilePath}=res let tipContent=open ? `文件保存成功,保存路径 ${savedFilePath},是否打开?` : `文件保存成功,保存路径 ${savedFilePath}` if(open){ showConfirm(tipContent).then(confirm=>{ uni.showLoading({title:'正在打开'}) uni.openDocument({ filePath:savedFilePath, fileType:'docx', success: function (openRes) { resolve(savedFilePath) }, complete:function (){ uni.hideLoading() } }) }) }else{ showAlert(tipContent) } }, fail:function (err){ console.log('保存失败') console.log(err) showAlert('保存失败',err) reject(err) }, complete:function (){ uni.hideLoading() } }) }, fail:function (err){ reject(err) }, complete:function (){ uni.hideLoading() } }) }) } 这里第一次打印url都还是doc文件,但是经过downloadFile success(res)里却变成了 pdf文件 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241011/a569a6bcaf3166003fc131385ec437f0.png)

阅读量:212

点赞量:0

问AI
1. 试试让后端通过"Content-Disposition" (https://link.segmentfault.com/?enc=3xtZ9Xcg2NcLoWzv7d0xow%3D%3D.Dsn7kM4wSs1Y%2BxVxdca%2BBMQQ3RhZvzXf%2BB1w%2FHJzR3cGnNdvKwDIor7q%2FpJCd8fRyh7HGU5gd26H5EEGCNQKeS25KoiLLlDrViQrpCNRYH4%3D)这个响应头来设置文件名,如"attachment; filename="xxx.docx"" 2. 如果可以是修改文件名的客户端,如微信小程序(看你截图像是),可以指定下文件名