#19505·taro

RN下的uploadFile中的createFormData方法存在name写死的问题

Author: StillAlexLiuCreated Sep 21, 2026Updated Sep 21, 2026
LabelsbugF-reactT-rnV-4

请先确认

  • 我已搜索并确定这个提交不是重复的

Taro 版本

v4

相关领域

No response

使用框架

React

相关平台

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 头条小程序
  • QQ 小程序
  • 钉钉小程序
  • 飞书小程序
  • 快手小程序

小程序基础库版本

No response

问题描述

下方是file.js的源码,可以看出fileObj的name被写死成了file,导致文件上传时,后端错误的获取了name,对于我的场景是,xxxx.jpg格式的文件,name为file,后端无法通过name获取正确的文件后缀区分文件类型

const createFormData = (filePath, body, name) => {
    const data = new FormData();
    const uri = isAndroid ? filePath : filePath.replace('file://', '');
    const fileObj = { uri: uri, type: 'application/octet-stream', name: 'file' };
    Object.keys(body).forEach(key => {
        data.append(key, body[key]);
    });
    // @ts-ignore
    data.append(name, fileObj);
    return data;
};

解决方法也很简单,只要在createFormData添加第四个参数fileName即可,uploadFile调用的时候fileName并没有被使用,并且文档上标出fileName只对h5生效,按照这种修改方式,可以在RN下生效了,即使name值在某些情况下有必须为file的必要,可以对其设置缺省值‘file’

复现链接

复现步骤

如上

环境信息

bash
 Taro v4.2.1


  Taro CLI 4.2.1 environment info:
    System:
      OS: macOS 27.0
      Shell: 5.9 - /bin/zsh
    Binaries:
      Node: 26.8.2 - /opt/homebrew/bin/node
      npm: 11.19.1 - /opt/homebrew/bin/npm
    npmPackages:
      @tarojs/cli: 4.2.1 => 4.2.1 
      @tarojs/components: 4.2.1 => 4.2.1 
      @tarojs/components-rn: 4.2.1 => 4.2.1 
      @tarojs/helper: 4.2.1 => 4.2.1 
      @tarojs/plugin-framework-react: 4.2.1 => 4.2.1 
      @tarojs/plugin-platform-h5: 4.2.1 => 4.2.1 
      @tarojs/plugin-platform-weapp: 4.2.1 => 4.2.1 
      @tarojs/react: 4.2.1 => 4.2.1 
      @tarojs/rn-runner: 4.2.1 => 4.2.1 
      @tarojs/rn-supporter: 4.2.1 => 4.2.1 
      @tarojs/runtime: 4.2.1 => 4.2.1 
      @tarojs/runtime-rn: 4.2.1 => 4.2.1 
      @tarojs/shared: 4.2.1 => 4.2.1 
      @tarojs/taro: 4.2.1 => 4.2.1 
      @tarojs/taro-loader: 4.2.1 => 4.2.1 
      @tarojs/taro-rn: 4.2.1 => 4.2.1 
      @tarojs/webpack5-runner: 4.2.1 => 4.2.1 
      babel-preset-taro: 4.2.1 => 4.2.1 
      eslint-config-taro: 4.2.1 => 4.2.1 
      expo: ~50.0.21 => 50.0.21 
      react: ^18.3.1 => 18.3.1 
      react-native: ^0.73.11 => 0.73.11

开源贡献