-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Native error is thrown when blob is read with FileReader.readAsDataURL #30378
Comments
Using |
Ran into the same problem today. I'm relatively sure the problem is here: https://github.com/facebook/react-native/blob/master/Libraries/Blob/RCTFileReaderModule.mm#L71 There is a null check for "type" on the subsequent line (defaulting it to application/octet-stream), but it actually should be prior to the RCTConvert, since that may fail if it is null and spit out an error. So in other words, I think this:
Should be something like:
I hacked around it by just setting blob.data.type to something manually before calling readAsDataURL. I'd try to submit a PR, but this is like my fourth day touching either React Native or ObjC, so somebody else should probably take it from here. |
أُرسلت من الـ iPad
… في 22/11/2020 الساعة 8:00 ص، كتب/كتبت Dan Menssen ***@***.***>:
Ran into the same problem today.
I'm relatively sure the problem is here:
https://github.com/facebook/react-native/blob/master/Libraries/Blob/RCTFileReaderModule.mm#L71
There is a null check for "type" on the subsequent line (defaulting it to application/octet-stream), but it actually should be prior to the RCTConvert, since that may fail if it is null and spit out an error.
So in other words, I think this:
NSString *type = [RCTConvert NSString:blob[@"type"]];
NSString *text = [NSString stringWithFormat:@"data:%@;base64,%@",
type != nil && [type length] > 0 ? type : @"application/octet-stream",
[data base64EncodedStringWithOptions:0]];
Should be something like:
NSString *blobType = blob[@"type"] != nil && [blob[@"type"] length] > 0 ? blob[@"type"] : @"application/octet-stream";
NSString *type = [RCTConvert NSString:blob[@"type"]];
NSString *text = [NSString stringWithFormat:@"data:%@;base64,%@",
type,
[data base64EncodedStringWithOptions:0]];
I hacked around it by just setting blob.data.type to something manually before calling readAsDataURL.
I'd try to submit a PR, but this is like my fourth day touching either React Native or ObjC, so somebody else should probably take it from here.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
@menssen I see. Shouldn't the |
in case @menssen's workaround wasn't clear, you can silence the error by manually setting a data type: fetch(src)
.then(res => res.blob())
.then(blob => {
// https://github.com/facebook/react-native/issues/30378
blob.data.type = 'image/jpeg';
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => {
var base64data = reader.result;
console.log(base64data);
};
})
.catch(e => console.error(e)); The problem being that the type information at the start of your base64 string will be whatever you set it to, not whatever the blob actually contained. Could be fine if you always know what kind of data you will be dealing with. This kind of thing really isn't my wheelhouse either, but you might be able to determine/repair the type information using something like: |
Any chance we can get some feedback with this ? |
it's the same issue with me , any solutions |
I found a solution to put blob response as a new Blob with desired type:
|
Any solutions to this? I am experiencing a similar problem on iOS utilizing react-native-webview. It throws the JSON value '' error only for a particular website that I navigate to in the webview. Other websites don't throw the error and it doesn't throw at all in Android. |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This issue was closed because it has been stalled for 7 days with no activity. |
I ran into this when the HTTP response was missing Content-Type header. Just update the server to include the content-type header to fix. |
Description
I'm using
FileReader.readAsDataURL
to consume aBlob
returned by RN's networking layer as anArrayBuffer
to implementFileReader.readAsArrayBuffer
(#21209) and it has been working just fine, at least with the data I've been processing.I'm working on an implementation of the Fetch API for RN where the underlying response is always a
ReadableStream
(soResponse.body
can be implemented at all times). All chunks of the stream areUint8Array
s and the only way at the moment to convert aBlob
to anArrayBuffer
is throughFileReader.readAsDataURL
.However, when I use
console.error
orconsole.warn
on iOS (didn't test on Android yet), the error shown in the image below is occurring.React Native version:
Steps To Reproduce
Provide a detailed list of steps that reproduce the issue.
Given any app, patch
Body.text()
in node_modules/whatwg-fetch/dist/fetch.umd.js as follows:Then, call
console.error
orconsole.warn
.Expected Results
I expect to be able to consume a
Blob
as a data URL without any error being thrown.The text was updated successfully, but these errors were encountered: