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

阅读 1.8k
2 个回答
  1. 试试让后端通过Content-Disposition这个响应头来设置文件名,如attachment; filename="xxx.docx"
  2. 如果可以是修改文件名的客户端,如微信小程序(看你截图像是),可以指定下文件名

1.url后缀为docx 文件内容不一定为docx
2.如果是微信小程序 下载时指定文件名呗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题