admin管理员组文章数量:1023089
I want to fetch audio-files from remote storage. How can i use the response-result from method "fetch" to handle binary data? i want to save them in local filesystem with react-native-fs. But i can't find nowhere a documentation of the return-value of method "fetch" to access the binary blob. I tried to console.log the response of fetch, but with binary data, xcode is hanging then being unresponsible
I am looking for something like this:
fetch(mediaUrl).then( (response) => {
let blob = response.getBinaryData()
})
(getBinaryData is'nt a valid method. And exactly this is my question: how to obtain the binary data?)
Update
some people points to "response.blob()", but this method is not implemented in react-native.
"response.blob is not a function. (In 'response.blob()', 'response.blob' is undefined)"
I want to fetch audio-files from remote storage. How can i use the response-result from method "fetch" to handle binary data? i want to save them in local filesystem with react-native-fs. But i can't find nowhere a documentation of the return-value of method "fetch" to access the binary blob. I tried to console.log the response of fetch, but with binary data, xcode is hanging then being unresponsible
I am looking for something like this:
fetch(mediaUrl).then( (response) => {
let blob = response.getBinaryData()
})
(getBinaryData is'nt a valid method. And exactly this is my question: how to obtain the binary data?)
Update
some people points to "response.blob()", but this method is not implemented in react-native.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 25, 2016 at 19:57 itinanceitinance 12.5k8 gold badges65 silver badges110 bronze badges"response.blob is not a function. (In 'response.blob()', 'response.blob' is undefined)"
2 Answers
Reset to default 2I've totally forgotten this (outdated) question of myself nearly one and a half year ago and will finally provide a suitable answer:
With the library react-native-fs one can download binary files and stores them binary on disk storage, that audio-files can played well with react-native-sound and video-files for instance can be played well with react-native-video.
Example code:
const downloadOptions = {
fromUrl: imageUrl,
toFile: filename
}
return yield RNFS.downloadFile(downloadOptions).promise.then( (res) => {
if(res.statusCode == 200) {
return true;
}
else {
console.log("Download failed", res);
return false;
}
})
.catch((err) => {
console.log('DownloadFile Error', err);
return false;
});
I spent a while investigating this. I'm sure there's more to know but I'd start with https://developer.mozilla/en-US/docs/Web/API/Blob and https://developer.mozilla/en-US/docs/Web/API/Body/blob
Here's a working RN code snippet.
url = 'https://www.google./images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
opts = {}
return fetch(url, opts)
.then(response => response.blob())
.then(blob => {
console.log('blob', blob);
let reader = new FileReader();
reader.addEventListener("loadend", function() {
console.log(reader.result);
});
reader.readAsDataURL(blob);
});
}
I want to fetch audio-files from remote storage. How can i use the response-result from method "fetch" to handle binary data? i want to save them in local filesystem with react-native-fs. But i can't find nowhere a documentation of the return-value of method "fetch" to access the binary blob. I tried to console.log the response of fetch, but with binary data, xcode is hanging then being unresponsible
I am looking for something like this:
fetch(mediaUrl).then( (response) => {
let blob = response.getBinaryData()
})
(getBinaryData is'nt a valid method. And exactly this is my question: how to obtain the binary data?)
Update
some people points to "response.blob()", but this method is not implemented in react-native.
"response.blob is not a function. (In 'response.blob()', 'response.blob' is undefined)"
I want to fetch audio-files from remote storage. How can i use the response-result from method "fetch" to handle binary data? i want to save them in local filesystem with react-native-fs. But i can't find nowhere a documentation of the return-value of method "fetch" to access the binary blob. I tried to console.log the response of fetch, but with binary data, xcode is hanging then being unresponsible
I am looking for something like this:
fetch(mediaUrl).then( (response) => {
let blob = response.getBinaryData()
})
(getBinaryData is'nt a valid method. And exactly this is my question: how to obtain the binary data?)
Update
some people points to "response.blob()", but this method is not implemented in react-native.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 25, 2016 at 19:57 itinanceitinance 12.5k8 gold badges65 silver badges110 bronze badges"response.blob is not a function. (In 'response.blob()', 'response.blob' is undefined)"
2 Answers
Reset to default 2I've totally forgotten this (outdated) question of myself nearly one and a half year ago and will finally provide a suitable answer:
With the library react-native-fs one can download binary files and stores them binary on disk storage, that audio-files can played well with react-native-sound and video-files for instance can be played well with react-native-video.
Example code:
const downloadOptions = {
fromUrl: imageUrl,
toFile: filename
}
return yield RNFS.downloadFile(downloadOptions).promise.then( (res) => {
if(res.statusCode == 200) {
return true;
}
else {
console.log("Download failed", res);
return false;
}
})
.catch((err) => {
console.log('DownloadFile Error', err);
return false;
});
I spent a while investigating this. I'm sure there's more to know but I'd start with https://developer.mozilla/en-US/docs/Web/API/Blob and https://developer.mozilla/en-US/docs/Web/API/Body/blob
Here's a working RN code snippet.
url = 'https://www.google./images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
opts = {}
return fetch(url, opts)
.then(response => response.blob())
.then(blob => {
console.log('blob', blob);
let reader = new FileReader();
reader.addEventListener("loadend", function() {
console.log(reader.result);
});
reader.readAsDataURL(blob);
});
}
本文标签: javascriptReact Native how to load binary file from remoteStack Overflow
版权声明:本文标题:javascript - React Native: how to load binary file from remote? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745535727a2154950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论