uni-app uni.uploadFile怎么突然报错 uploadFile fail file error

uni-app uni.uploadFile怎么突然报错 uploadFile fail file error

开发环境 版本号 项目创建方式
Windows Windows10 HBuilderX

产品分类:uniapp/H5


PC开发环境操作系统:Windows

HBuilderX类型:正式
HBuilderX版本号:4.87


浏览器平台:Chrome
浏览器版本:360安全浏览器16,16.1.1139.64(64位)


示例代码:

const imgBlob = await fetch(lists[i].url).then(r => r.blob()) const imgFile = new File([imgBlob], lists[i].name, { type: imgBlob.type }) uni.uploadFile({ url: apiUrl, file: file, fileType:‘image’, name: name, header: { ‘Authorization’: uni.getStorageSync(‘token’) ? Bearer ${uni.getStorageSync('token')} : ‘’ }, success: (res) => { let result = JSON.parse(res.data) console.log(result); if (result.code !== ‘00000’) { uni.showToast({ title: result.msg, icon: ‘error’ }) reject(res) } else { resolve(res) } }, fail: (err) => { reject(err) } });


更多关于uni-app uni.uploadFile怎么突然报错 uploadFile fail file error的实战教程也可以访问 https://www.itying.com/category-93-b0.html

22 回复

该bug反馈内容基本完整但存在关键缺失:未明确说明运行平台(App/Web/小程序类型),而这是分析uni.uploadFile问题的核心。代码示例存在明显问题——uni.uploadFile的file参数在非Web平台(如App、小程序)需传入本地文件路径字符串,而非File对象(仅Web支持File/Blob)。用户混淆了多端差异:在App平台应使用plus.io.convertLocalFileSystemURL获取路径,而非直接构造File对象。
bug成立但非版本异常:知识库uni.uploadFile文档明确说明各平台实现差异,小程序需配置域名白名单,App平台不支持直接传入File对象。用户遇到的"file error"正是因在非Web环境错误使用File对象导致,与HBuilderX 4.87版本无关(该版本正常支持上传功能)。
建议解决方案:

若为App平台:改用uni.saveFile保存临时文件后上传路径
若为小程序:检查后台域名是否已加入白名单
统一处理:优先使用uni-app官方推荐方案对接uniCloud,避免平台兼容性问题

反馈中"后台返回200但进fail"符合逻辑:客户端因文件参数格式错误提前终止上传,未真正发送请求,故不会触发服务端响应。需补充平台信息和完整错误日志以便精准定位。 内容为 AI 生成,仅供参考

更多关于uni-app uni.uploadFile怎么突然报错 uploadFile fail file error的实战教程也可以访问 https://www.itying.com/category-93-b0.html


filePath 和 files 不能同时为空,必须有一个

之前都是传的file参数,是Blob格式的文件,files是传什么?

回复 m***@qq.com: 看文档

回复 DCloud_UNI_JBB: 请问是不是依赖库的版本改了?file参数不用了?之前能用的,现在要换成filePath,后台接口也得跟着改,很麻烦的。

回复 m***@qq.com: 之前这里有问题,修复了一个bug,你要是不好改,可以先回退hx版本

回复 DCloud_UNI_JBB: 好的。HBuilder需要退回到哪个版本?

回复 m***@qq.com: 4.76 你试试

回复 DCloud_UNI_JBB: 好的,是不是要重新下载hx?有没有命令降版本?

回复 m***@qq.com: 4.76下载解压后运行,报插件【uni-app(vue2)编译器】下载失败,网络请求失败(SsIHandshakeFailedError),这是什么情况?

回复 DCloud_UNI_JBB: HBuilderX能不能不要这么坑人啊!修复bug这么不严谨吗?

// #ifdef H5 return new Promise((resolve, reject) => { uni.uploadFile({ url: apiUrl, file: file, fileType:‘image’, name: name, header: { ‘Authorization’: uni.getStorageSync(‘token’) ? Bearer ${uni.getStorageSync(‘token’)} : ‘’ }, success: (res) => { let result = JSON.parse(res.data) console.log(result); if (result.code !== ‘000’) { uni.showToast({ title: result.msg, icon: ‘error’ }) reject(res) } else { resolve(res) }
},
fail: (err) => {
reject(err)
}
});
}) // #endif
完整代码如上,是在H5模式下。

把 file 去掉,改为 filepath,也就是传入文件本地地址方式,别传入文件对象方式,我也出现了,改为地址方式就正常了

改成filepath,后台接口也需要跟着改吧?

不需要

我先试一下,即便后台不改,前端所有调用的地方也都要改,太麻烦了。。。。

试过确实不用改后台,直接把file参数名改成filePath,参数值直接传url就可以。只是这也会导致版本整个要调整,简直是现网事故啊!HBuilderX怎么插件升级都不向下兼容的,说的不恰当点,有点像故意搞破坏了。。。

降HBuilderX能解决,我情愿降版本了,不然代码改动太大了,崩溃啊。。。太不靠谱了

回复 m***@qq.com: 淡定

回复 空白人吗: 嗯,还是淡定下来把涉及的地方都改了。。还好这个版本影响面还不算大。

回复 m***@qq.com: 这里本来是有bug,才导致了只传file参数也能运行

这个错误通常是由于 uni.uploadFilefile 参数格式不正确导致的。从你的代码看,问题出在 file: file 这一行。

问题分析:

  1. 你创建的是 File 对象(imgFile),但上传时却传入了 file 变量
  2. 在示例代码中,你定义了 imgFile 但实际使用的是未定义的 file 变量

解决方案: 修改 uni.uploadFile 调用中的 file 参数:

// 将 file: file 改为:
file: imgFile,  // 使用你创建的 imgFile 对象

完整修正后的代码:

const imgBlob = await fetch(lists[i].url).then(r => r.blob())
const imgFile = new File([imgBlob], lists[i].name, {
    type: imgBlob.type
})

uni.uploadFile({
    url: apiUrl,
    file: imgFile,  // 这里改为 imgFile
    fileType: 'image',
    name: name,
    header: {
        'Authorization': uni.getStorageSync('token') ? `Bearer ${uni.getStorageSync('token')}` : ''
    },
    success: (res) => {
        let result = JSON.parse(res.data)
        console.log(result);
        if (result.code !== '00000') {
            uni.showToast({
                title: result.msg,
                icon: 'error'
            })
            reject(res)
        } else {
            resolve(res)
        }
    },
    fail: (err) => {
        reject(err)
    }
});
回到顶部