Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] FormData does not support data URIs #25790

Closed
mxhold opened this issue Jul 23, 2019 · 25 comments
Closed

[Android] FormData does not support data URIs #25790

mxhold opened this issue Jul 23, 2019 · 25 comments
Labels
Bug Platform: Android Android applications.

Comments

@mxhold
Copy link

mxhold commented Jul 23, 2019

React Native version: Expo 33 / React Native 0.59.8

Steps To Reproduce

  1. Call formData.append with a data URI as the uri
  2. Make XHR request and send FormData
const dataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=';
const formData = new FormData();
formData.append('file', {
  name: 'myfile',
  type: 'image/png',
  uri: dataUri,
});

const request = new XMLHttpRequest();
request.open('POST', 'http://httpbin.org/post');
request.onreadystatechange = function() {
  if (request.readyState === 4) {
    if (request.status === 200) {
      console.log("SUCCESS")
      // will succeed on iOS
    } else {
      console.log(request.responseText);
      // response on Android will be "Could not retrieve file for uri data:image/png;base64,iVBO..."
    }
  }
};
request.send(formData);

Describe what you expected to happen:

The request would succeed (as it does on iOS) but instead (on Android only), you get this error:

Could not retrieve file for uri data:image/png;base64,iVB...

Snack: https://snack.expo.io/@maxholderc5/cd0ef5

It appears that the Android implementation assumes this will always be a file, but unless I've misunderstood, passing a data URI here should also be valid (at least, it seems to work on iOS).

A workaround for my specific scenario (in case it is helpful for others running into this) was to write a temporary file and provide its filepath instead of the data URI directly, e.g. with Expo:

const tempFileDirectory = `${FileSystem.cacheDirectory}uploadTmp`
await FileSystem.makeDirectoryAsync(tempFileDirectory, { intermediates: true })
const tempFilePath = `${tempFileDirectory}${Date.now()}.png`
const base64EncodedData = dataUri.split(',')[1]
await FileSystem.writeAsStringAsync(tempFilePath, base64EncodedData, {
  encoding: FileSystem.EncodingType.Base64,
})
// ...
formData.append(name, {
  name: "myfile",
  type: "image/png",
  uri: tempFilePath,
})
// ...
await FileSystem.deleteAsync(tempFilePath)
@mxhold mxhold added the Bug label Jul 23, 2019
@react-native-bot react-native-bot added the Platform: Android Android applications. label Jul 23, 2019
@zcgit
Copy link

zcgit commented Jul 27, 2019

Same question, I need to send a memory data as A file.

@stale
Copy link

stale bot commented Oct 25, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 25, 2019
@Cookizza
Copy link

This is still an issue

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 31, 2019
@stale
Copy link

stale bot commented Jan 30, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 30, 2020
@mxhold
Copy link
Author

mxhold commented Jan 30, 2020

This is still an issue

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 30, 2020
@stale
Copy link

stale bot commented Apr 29, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 29, 2020
@mxhold
Copy link
Author

mxhold commented Apr 30, 2020

This is still an issue

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Apr 30, 2020
@irekrog
Copy link

irekrog commented May 14, 2020

This is still an issue :/

@m7amad-7asan
Copy link

This is still an issue

3 similar comments
@patrickyyang
Copy link

This is still an issue

@Ping5841
Copy link

Ping5841 commented Jul 9, 2020

This is still an issue

@evanc
Copy link

evanc commented Aug 5, 2020

This is still an issue

@irekrog
Copy link

irekrog commented Aug 6, 2020

This bug is over a year old. Is anyone from RN team considering solving this bug?

@stale
Copy link

stale bot commented Dec 26, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 26, 2020
@tlrobinson
Copy link

This is still an issue

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 26, 2021
@glaucopgomes
Copy link

This is still an issue

4 similar comments
@juanloaiza21
Copy link

This is still an issue

@vgorte
Copy link

vgorte commented Oct 19, 2021

This is still an issue

@jfr3000
Copy link

jfr3000 commented Jan 21, 2022

This is still an issue

@anhkieet
Copy link

This is still an issue

@dxmv
Copy link

dxmv commented Feb 16, 2022

This is still an issue

2 similar comments
@Mickaz89
Copy link

This is still an issue

@pepe-dunamu
Copy link

This is still an issue

@hetanthakkar
Copy link
Contributor

I'll work on this issue

@Alireza17224
Copy link

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Platform: Android Android applications.
Projects
None yet