Skip to content

[Android] FormData fails to send data in multipart/form-data #5308

@ashleydw

Description

@ashleydw

As per #1357 (comment) @philikon implemented uploading files (images) via the NetworkingModule.java.

The following works on iOS (image and description are sent to server and received correctly) on iOS. On Android, the request is made but both image and description are missing from the request.

let formData = new FormData();
formData.append('image', {uri: image.uri, type: 'image/jpg', name: 'image.jpg'});
formData.append('description', String(data.description));

let options = {};
options.headers['Content-Type'] = 'multipart/form-data; boundary=6ff46e0b6b5148d984f148b6542e5a5d';
options.body = formData;
return fetch(uri, options).then((response) => {
   .... 
});

Where image.uri is either an android or ios file:

  • android: file:///storage/emulated/0/Pictures/eb645893-4c00-44a3-a9b4-a2116e955f7c.jpg
  • ios: /Users/ashleydw/Library/Developer/CoreSimulator/Devices/23EE88D0-6E91-43AD-A3B6-06F87698C5A8/data/Containers/Data/Application/A73E68D3-7424-4301-9934-AD0F8251C1EB/tmp/7803DA8A-0E40-4FCB-A593-884805878172.jpg

I've tried both with and without it file://. Without it, the NetworkingModule.java reaches "Could not retrieve file for uri /storage/emulated/0/Pictures/8505b7c8-389e-4a0a-8d0a-7669891031f9.jpg" on line https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java#L166.

With it, the requests send but the data is missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions