fix(rn): uploadFile sends the real file name instead of hardcoded 'file' by dvd233 · Pull Request #19507 · NervJS/taro · GitHub
Skip to content

fix(rn): uploadFile sends the real file name instead of hardcoded 'file' - #19507

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
dvd233:fix/rn-uploadfile-filename-19505
Sep 29, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
dvd233:fix/rn-uploadfile-filename-19505

Conversation

@dvd233

@dvd233 dvd233 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / 这个 PR 做了什么

On React Native, uploadFile builds the multipart file object with a hardcoded name: 'file', so the server always receives the file name file and cannot read the original name or extension (e.g. to distinguish image types), as reported in #19505.

RN 端 uploadFile 组装 multipart 文件对象时把 name 写死为 'file',后端拿不到真实文件名/后缀(#19505)。

The H5 implementation already sends a meaningful file name (form.append(name, fileObj, fileName || \file-${Date.now()}`)), and the public Taro.uploadFile.Optiontype already exposesfileName`. RN is the outlier.

How / 修改方式

packages/taro-rn/src/lib/file.ts — createFormData now resolves the file name as:

  1. the explicit fileName option, then
  2. the filePath basename (e.g. xxxx.jpg), then
  3. the historical 'file' fallback when neither exists.

packages/taro/types/api/network/upload.d.ts — fileName 的 @supported 标记从 h5 更新为 h5, rn(该选项类型本来就存在,仅文档标记未覆盖 RN)。

Verification / 验证

  • Red: with the fix reverted, the two new assertions fail (name is always 'file'); the fallback case passes on both old and new code, pinning backward compatibility.
  • Green: packages/taro-rn full Jest suite — 14 suites / 36 passed / 1 skipped, including the new __tests__/uploadFile.test.ts (basename derivation, explicit fileName override, 'file' fallback).
  • tsc --noEmit on packages/taro-rn: zero errors in changed files (the single remaining error is the pre-existing @tarojs/runtime-rn workspace build-order issue in untouched src/api/index.ts, identical before and after).
  • ESLint on changed files: 0 errors; git diff --check clean.

Note: the package's own testMatch glob does not match on Windows (mixed separators), so tests were run with npx jest --testMatch '**/__tests__/**/*.test.ts' — same suites, no repo config changes.

Fixes #19505

Summary by CodeRabbit

  • 新功能
    • React Native 平台的文件上传现支持自定义 fileName。
    • 未指定文件名时,系统会优先使用文件路径中的名称;无法解析时回退为默认名称。
    • 上传文件类型统一标记为 application/octet-stream。
  • 测试
    • 新增文件上传相关测试,覆盖自定义文件名、路径名称提取及默认名称回退场景。

@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.39%. Comparing base (0f74829) to head (0e3202a).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19507      +/-   ##
==========================================
- Coverage   54.66%   54.39%   -0.27%     
==========================================
  Files         465      466       +1     
  Lines       25392    25551     +159     
  Branches     6364     6361       -3     
==========================================
+ Hits        13880    13898      +18     
- Misses       9636     9667      +31     
- Partials     1876     1986     +110     
Flag Coverage Δ
taro-cli 56.96% <ø> (ø)
taro-runtime 58.11% <ø> (ø)
taro-web 52.37% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-rn/src/lib/file.ts 11.32% <100.00%> (ø)

... and 53 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer
Single-Dancer force-pushed the fix/rn-uploadfile-filename-19505 branch from ccc5cc5 to b0f096b Compare September 29, 2026 03:32
On React Native, createFormData hardcoded the multipart file object's
name to 'file', so backends could not read the original file name or
extension (e.g. to distinguish image types). The H5 implementation
already forwards a meaningful name (option fileName, blob name, or a
generated fallback).

RN now resolves the name as: explicit fileName option, then the
filePath basename, then the historical 'file' fallback. The public
Taro.uploadFile.Option type already exposes fileName; its @supported
tag is updated to include rn.

Adds regression tests covering the basename derivation, the explicit
fileName override, and the 'file' fallback.

Fixes NervJS#19505
@Single-Dancer
Single-Dancer force-pushed the fix/rn-uploadfile-filename-19505 branch from b0f096b to 0e3202a Compare September 29, 2026 06:29
@Single-Dancer
Single-Dancer merged commit 73d9cb0 into NervJS:main Sep 29, 2026
25 checks passed
@Single-Dancer Single-Dancer added this to the 4.2.2 milestone Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants