1

I can't seem to get this to work i know. the "onFileDownload={ ( { nativeEvent: { downloadUrl } } ) => {" has been added and i need my own code to make this work. I tried to use expo file system and expo sharing to get this to download but it's not working. all i want is that when they download a pdf instead of a preview they can share the document or save it. Any help would be greatly appreciated.

Below is the code i tried to make it work but failed (the alert function works but then nothing happens):


import React, { Component } from 'react';
import { StyleSheet, ActivityIndicator, View, Platform, PermissionsAndroid, Alert } from 'react-native';
import * as Permissions from 'expo-permissions';
import { WebView } from 'react-native-webview';
//sharing and download
import * as Sharing from 'expo-sharing';
import * as FileSystem from 'expo-file-system';
import { Image, Text, TouchableOpacity, } from 'react-native';
import * as ImagePicker from 'expo-image-picker';

export default function App() {
let [selectedImage, setSelectedImage] = React.useState(null);

let openImagePickerAsync = async () => {
const downloadResumable = FileSystem.createDownloadResumable(
{downloadUrl},
${FileSystem.documentDirectory}/pdf.pdf,
{},
);

const { uri, status } = await downloadResumable.downloadAsync();
// setSelectedImage({ localUri: uri });
Sharing.shareAsync(uri);
};

let openShareDialogAsync = async () => {
if (!(await Sharing.isAvailableAsync())) {
alert(Uh oh, sharing isn't available on your platform);
return;
}

Sharing.shareAsync(selectedImage.localUri);
};

// if (selectedImage !== null) {
// return (
// 
// 
// Share this
// 
// 
// );
// }

return (
<View style={{ flex: 1 }}>
<WebView
style={{ flex: 1 }}
source={{ uri: 'http://www.pdf995.com/' }}
onFileDownload={ ( { nativeEvent: { downloadUrl } } ) => {

        Alert.alert(
          "Documents",
          "Do you wish to download and share this document",
          [
            {
              text: "Download",
              onPress: () => {openImagePickerAsync}
            },
            {
              text: "Cancel",
              onPress: () => console.log("Cancel Pressed"),
              style: "cancel"
            },
          ],
          { cancelable: false }
        );}
      } />
      </View>
    );
  }

I took the code from expo snack: https://snack.expo.io/@trinet/sharing Any help would be greatly appreciated. I'm really stuck

1 Answers1

0

I had the same experience, in my case, it was due to some wrong header config, Content-Disposition inline instead of an attachment, so what happened is that inline attribute makes it render the content in the web page whereas attachment sees a file and tries to download it without previewing.

Add this in the header config of the file on the server and you will be file. Credits to this SO answer and this

Eddie
  • 31
  • 1
  • 5