How can I download through WebView using react-native?
How can I download through WebView using react-native?
I'm using a WebView
and I want to let the person download through the WebView
.
It's a simple WebView
that have a link to download a zip file.
WebView
WebView
WebView
Can I do using the webview? (Like Chrome)
at least I want to get the listener when the user try to download the file though the WebView
.
WebView
2 Answers
2
I did on this way, to download a file we need two steps, fetch the bits and create a file on device, in this example I'm downloading any .zip
file when the url have a .zip
:
.zip
.zip
I'm using :
import RNFetchBlob from 'rn-fetch-blob';
var RNFS = require('react-native-fs');
and using the WebView
like this:
WebView
<WebView
source= uri: "http://myinitialurl.com.br/arquivos/"
style=
startInLoadingState=true
allowUniversalAccessFromFileURLs=true
javaScriptEnabled=true
mixedContentMode='always'
onNavigationStateChange=(result) => this.handleUrlWithZip(result)
/>
and:
handleUrlWithZip(input)
//check if have another download
if (this.state.downloadStart == true
There is aa drop-in replacement component for the original WebView
https://github.com/react-native-community/react-native-webview which supports file downloads (and uploads) since December 2018.
WebView
All you need is to add the permissions to iOS by adding the following to ios/[project]/Info.plist
:
ios/[project]/Info.plist
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Save pictures for certain activities.</string>
or for Android by adding this to the AndroidManifest.xml
AndroidManifest.xml
<!-- this is required to save files on Android -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy