微信小程序下载文件重命名

/**
 * @description downloadFile 下载文件
 * @param {string} url 下载链接 
 * @param {string} text 文件名
 * @param {string} suffix 后缀名 例如:.pdf
 */
function (url,text){
    const i = url.lastIndexOf('.')
    const suffix = url.slice(i,url.length)
    const openType = url.slice(i + 1,url.length)
    wx.downloadFile({
        url: link?link:this.data.url,
        filePath:`${wx.env.USER_DATA_PATH}/${text}${suffix}`, // 需要预览文件的名称,
        success: res => {
            const filePath = res.filePath  // 关键
            wx.openDocument({
                fileType: openType,
                filePath: filePath,
                fail(e) {
                  console.log("打开失败")
                }
            })
        },fail() {
            console.log("下载失败")
        }
    })
}

注:ios不生效

猜你喜欢

转载自blog.csdn.net/m0_65894854/article/details/132231231