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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)