You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用React Native Fetch Blob上传图片后文件0字节且APP崩溃求助

Fixing RNFetchBlob Image Upload Issues (0-byte File + App Crash)

Hey there, let’s work through this frustrating upload problem you’re hitting. A 0-byte uploaded file and app crashes are classic signs of either incorrect file handling, missing permissions, or network misconfiguration. Let’s break down the fixes step by step:

1. Verify Your File Path is Correct

The most common culprit here is that this.state.data isn’t a valid local file path. RNFetchBlob.wrap() requires an absolute path to the image file (e.g., something like file:///storage/emulated/0/Pictures/image.png on Android, or file:///var/mobile/Containers/Data/Application/.../image.png on iOS).

  • If you’re using an image picker (like react-native-image-picker), make sure you’re storing the uri property from the picker response, not a base64 string or relative path.
  • Add a quick check before uploading to confirm the path exists:
    RNFetchBlob.fs.stat(this.state.data)
      .then(stats => {
        console.log("File size:", stats.size); // Should be > 0 if valid
      })
      .catch(err => console.error("File not found:", err));
    

2. Fix Your Network Target

Using localhost won’t work on a physical device—it points to the device itself, not your computer’s server. Replace localhost with your computer’s local network IP address (e.g., http://192.168.1.105/uploadingImage.php). You can find this IP in your computer’s network settings.

3. Ensure Proper Permissions

Without file access permissions, RNFetchBlob can’t read the image file, leading to empty data and crashes:

  • Android: Add this to your AndroidManifest.xml:
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <!-- For Android 13+, use READ_MEDIA_IMAGES instead -->
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    
  • iOS: Add this to your Info.plist:
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Need access to photos to upload images</string>
    

4. Add Error Handling

Missing error catching can cause silent crashes when something goes wrong. Always append a .catch() block to your fetch call to log issues.

Corrected Code Example

Here’s your code with all these fixes applied:

uploadingPhotos() {
  const imagePath = this.state.data;
  
  // Guard clause for invalid path
  if (!imagePath) {
    console.error("No image path provided!");
    return;
  }

  RNFetchBlob.fetch(
    'POST',
    'http://192.168.1.105/uploadingImage.php', // Replace with your local IP
    {
      Authorization: "Bearer access-token",
      otherHeader: "foo",
      'Content-Type': 'multipart/form-data',
    },
    [
      {
        name: 'image',
        filename: 'image.png',
        type: 'image/png',
        data: RNFetchBlob.wrap(imagePath)
      },
    ]
  )
  .then((resp) => {
    console.log("Upload successful:", resp.text());
    // Handle your success logic here
  })
  .catch((err) => {
    console.error("Upload failed:", err.message);
    // Show an error message to the user here
  });
}

Bonus: iOS ATS Configuration

If your server uses HTTP (not HTTPS), you’ll need to allow insecure connections in iOS by adding this to your Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

(Note: For production apps, use specific domain exceptions instead of allowing arbitrary loads.)

内容的提问来源于stack exchange,提问作者Pots

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:47:19