收藏
回答

为什么我点击后,总是报错获取文件链接失败?

Page({  

  // ...  

    

  downloadFile: function(e) {  

    const fileID = e.currentTarget.dataset.fileid;  

    wx.cloud.callFunction({  

      name: 'generateTempLink',  

      data: {  

        fileID: fileID,  

      },  

      success: res => {  

        if (res.result.tempFileURL) {  

          // 使用临时链接下载文件  

          wx.downloadFile({  

            url: res.result.tempFileURL,  

            success: function(res) {  

              const filePath = res.tempFilePath;  

              // 可以选择保存文件到本地或者做其他处理  

              wx.saveFile({  

                tempFilePath: filePath,  

                success(res) {  

                  wx.showToast({  

                    title: '文件已保存',  

                    icon: 'success',  

                    duration: 2000,  

                  });  

                },  

                fail(err) {  

                  wx.showToast({  

                    title: '保存失败',  

                    icon: 'none',  

                    duration: 2000,  

                  });  

                }  

              });  

            },  

            fail: function(err) {  

              wx.showToast({  

                title: '下载失败',  

                icon: 'none',  

                duration: 2000,  

              });  

            },  

          });  

        } else {  

          wx.showToast({  

            title: '获取临时链接失败',  

            icon: 'none',  

            duration: 2000,  

          });  

        }  

      },  

      fail: err => {  

        wx.showToast({  

          title: '云函数调用失败',  

          icon: 'none',  

          duration: 2000,  

        });  

        console.error('[云函数] 调用失败', err);  

      },  

    });  

  },  

}); 这个是js文件

const cloud = require('wx-server-sdk');  

cloud.init();  

 cloud.init({  

  // 设置云开发环境id,如果设置了DYNAMIC_CURRENT_ENV常量,则会自动获取当前环境  

  env: cloud.DYNAMIC_CURRENT_ENV  

});  

  

// 云函数入口函数  

exports.main = async (event, context) => {  

  // 你的云函数代码逻辑  

  return { message: 'Hello from cloud function!' };  

};

const tcb = cloud.callFunction({  

  name: 'resumableDownload', // 需要先部署这个云函数,用于处理大文件的分片下载  

  data: {},  

});  

 

exports.main = async (event, context) => {  

  const fileID = event.fileID;  

  try {  

    const res = await cloud.getTempFileURL({  

      fileList: [fileID],  

      action: 'download',  

    });  

    return {  

      tempFileURL: res.fileList[0].tempFileURL,  

    };  

  } catch (err) {  

    console.error(err);  

    return {  

      error: err,  

    };  

  }  

};这个是云函数js文件

回答关注问题邀请回答
收藏

1 个回答

  • 一笑皆春
    一笑皆春
    04-16

    打印一下 const fileID = e.currentTarget.dataset.fileid; 看看,链接对吗

    04-16
    有用
    回复 2
    • 记得带伞
      记得带伞
      04-16
      链接我是直接在云存储里复制的
      04-16
      回复
    • 一笑皆春
      一笑皆春
      04-16回复记得带伞
      一步步的打印调试看看,看看走到哪一步报错的
      04-16
      回复
登录 后发表内容