uni-app uni.uploadFile 小程序使用不传递参数

uni-app uni.uploadFile 小程序使用不传递参数

示例代码:

uni.uploadFile({
url: uni.getStorageSync('uploadUrl') +
'/appapi/imports/upload-img', // 仅为示例,非真实的接口地址
filePath: url,
name: 'file',
formData: {
'Token': uni.getStorageSync('TOKEN')
},
success: (res) => {
// console.log(res)
if (JSON.parse(res.data).code != 0) {
this.$pop(JSON.parse(res.data).message)
this.fileList1 = [];
return
} else {
resolve(JSON.parse(res.data).data.imgPath)
}
}
})

操作步骤:

直接调用uni.uploadFile传参

预期结果:

小程序正常上传

实际结果:

小程序不能使用

bug描述:

h5正常 小程序不能传参 如下图

Image

Image



| 信息类别         | 内容                     |
|------------------|--------------------------|
| 产品分类         | uniapp/小程序/微信       |
| PC开发环境操作系统 | Windows                  |
| PC开发环境操作系统版本号 | Windows11                |
| HBuilderX类型    | 正式                     |
| HBuilderX版本号  | 3.8.7                    |
| 第三方开发者工具版本号 | 1.06                     |
| 基础库版本号     | 2.32.2                   |
| 项目创建方式     | HBuilderX                |

更多关于uni-app uni.uploadFile 小程序使用不传递参数的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

已解决

更多关于uni-app uni.uploadFile 小程序使用不传递参数的实战教程也可以访问 https://www.itying.com/category-93-b0.html


求问怎么解决的

求问怎么解决的啊

根据描述和截图,问题出在小程序环境下uni.uploadFile的formData参数传递失败。这是微信小程序平台的常见限制,解决方案如下:

  1. 确保formData中的参数都是字符串类型,微信小程序要求formData值必须是String类型

  2. 检查小程序后台配置的合法域名是否包含上传地址

  3. 修改代码为:

uni.uploadFile({
  url: 'https://yourdomain.com/upload',
  filePath: url,
  name: 'file',
  formData: {
    'Token': String(uni.getStorageSync('TOKEN')) // 显式转换为字符串
  },
  success: (res) => {
    // 处理结果
  }
})
  1. 如果仍然不行,可以尝试将Token放在header中传递:
uni.uploadFile({
  url: 'https://yourdomain.com/upload',
  filePath: url,
  name: 'file',
  header: {
    'Token': uni.getStorageSync('TOKEN')
  },
  success: (res) => {
    // 处理结果
  }
})
回到顶部