微信小程序wx.downloadFile报错 no such file or directory

微信小程序wx.downloadFile报错 no such file or directory

function down_file() {
   
   
  var _this = this;
  var path = wx.getStorageSync('savedFilePath')
  if(path) {
   
   
    console.log(path)
    accessFile(path)
    return
  }
  
  const downloadTask=wx
### 解析 uni.downloadFile 报错 ENOENT 当遇到 `uni.downloadFile` 出现 `fail Error: ENOENT: no such file or directory` 的错误时,通常意味着尝试访问的文件或目录不存在。此问题可能由多种原因引起。 #### 文件路径验证 确保指定的目标存储路径确实存在并可写入。如果目标路径中的某个父级目录缺失,则会触发此类错误[^3]。因此,在执行下载操作前应先确认最终保存位置的有效性: ```javascript const fs = require('fs'); const path = require('path'); function ensureDirectoryExistence(filePath) { const dirname = path.dirname(filePath); if (fs.existsSync(dirname)) { return true; } ensureDirectoryExistence(dirname); fs.mkdirSync(dirname); } ``` 对于 UniApp 应用程序而言,可以利用其内置 API 来获取合适的本地缓存路径作为默认下载目的地: ```javascript // 获取应用内部临时文件夹路径 let tempFilePath = wx.getStorageSync('_wxTempPath') || ''; if (!tempFilePath.length) { tempFilePath = `${plus.io.convertLocalFileSystemURL(plus.os.name === "Android" ? "_doc/" : "")}Documents/`; } ensureDirectoryExistence(tempFilePath); // 使用上述准备好的路径发起下载请求 uni.downloadFile({ url: 'http://example.com/file.zip', // 下载资源链接 filePath: `${tempFilePath}/file.zip`, // 存储地址 success(res) { console.log(`Download succeeded! File saved at ${res.tempFilePath}`); }, fail(err) { console.error('Download failed:', err.errMsg); } }); ``` 通过以上方法可以在一定程度上预防因路径不正确而导致的 ENOENT 错误发生。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值