uni-app createDownload 下载失败没有回调

uni-app createDownload 下载失败没有回调

开发环境 版本号 项目创建方式
HBuilderX 3.3.11 云端

产品分类:HTML5+

手机系统:Android

手机系统版本号:Android 11

手机厂商:小米

手机机型:小米11


示例代码:

let url = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Faec8564cad246463f63a5c4a8ec62871365e98'  

let dtask = plus.downloader.createDownload( url, {  
    filename: "file://storage/emulated/0/file.png"   
},  
(downFile, status) => {  
    if (status == 200) {  
        uni.hideLoading()  
        uni.showToast({  
            title: '下载成功'  
        })  
    } else {  
        //下载失败  
        uni.hideLoading();  
        uni.showToast({  
            icon: 'none',  
            title: `文件不存在或已被删除`  
        });  
        plus.downloader.clear(); //清除下载任务  
    }  
})  
dtask.start();

操作步骤:

  • 下载一个服务器上没有的文件

预期结果:

  • 正常返回404状态码

实际结果:

  • 无下载失败回调

bug描述:

let url = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Faec8564cad246463f63a5c4a8ec62871365e98'  

let dtask = plus.downloader.createDownload( url, {  
    filename: "file://storage/emulated/0/file.png"   
},  
(downFile, status) => {  
    if (status == 200) {  
        uni.hideLoading()  
        uni.showToast({  
            title: '下载成功'  
        })  
    } else {  
        //下载失败  
        uni.hideLoading();  
        uni.showToast({  
            icon: 'none',  
            title: `文件不存在或已被删除`  
        });  
        plus.downloader.clear(); //清除下载任务  
    }  
})  
dtask.start();

更多关于uni-app createDownload 下载失败没有回调的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

这个社区根本没有官方回复吧,社区一大堆人问问题,没人理

更多关于uni-app createDownload 下载失败没有回调的实战教程也可以访问 https://www.itying.com/category-93-b0.html


1、状态码监听statechanged事件查看 相关文档:plus.downloader.DownloadEvent 2、filename只支持应用内的"_downloads/"、"_doc/"、"_documents/" 3、downloader会进行多次尝试连接,尝试次数结束后才会走回调

uni-app 中,使用 uni.downloadFile 进行文件下载时,如果下载失败,通常会有相应的回调函数来处理错误。如果你发现下载失败时没有回调,可能是以下几个原因导致的:

1. 检查回调函数是否正确绑定

确保你在 uni.downloadFile 中正确绑定了 fail 回调函数。例如:

uni.downloadFile({
  url: 'https://example.com/file.zip',
  success: (res) => {
    console.log('下载成功', res);
  },
  fail: (err) => {
    console.error('下载失败', err);
  },
  complete: (res) => {
    console.log('下载完成', res);
  }
});

2. 检查网络请求是否被拦截

有些情况下,网络请求可能会被浏览器或应用本身的安全策略拦截,导致请求无法发出。你可以通过 uni.request 先测试一下网络请求是否正常。

3. 检查 URL 是否正确

确保你提供的下载 URL 是正确的,并且服务器能够正常响应。如果 URL 错误或服务器无法访问,可能会导致下载失败。

4. 检查跨域问题

如果你的下载请求涉及到跨域问题,可能会导致请求失败。确保服务器配置了正确的 CORS 策略。

5. 检查文件大小限制

某些平台(如微信小程序)对下载文件的大小有限制。如果文件过大,可能会导致下载失败。

6. 检查平台差异

不同平台(如 H5、小程序、App)对 uni.downloadFile 的实现可能有所不同。确保你在目标平台上测试了代码。

7. 使用 complete 回调

如果 fail 回调没有触发,可以尝试使用 complete 回调来捕获所有情况:

uni.downloadFile({
  url: 'https://example.com/file.zip',
  success: (res) => {
    console.log('下载成功', res);
  },
  fail: (err) => {
    console.error('下载失败', err);
  },
  complete: (res) => {
    if (res.statusCode !== 200) {
      console.error('下载失败', res);
    }
  }
});
回到顶部