尝试从React Native Webview下载pdf

时间:2020-09-10 17:53:06

标签: react-native expo react-native-webview

我知道我似乎无法正常工作。已添加“ onFileDownload = {({nativeEvent:{downloadUrl}})=> {”,我需要自己的代码才能完成此工作。我试图使用expo文件系统和expo共享来下载此文件,但它不起作用。我想要的是,当他们下载pdf而不是预览时,他们可以共享或保存文档。任何帮助将不胜感激。

下面是我试图使其工作但失败的代码(警报功能起作用,但是什么也没发生):


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>
    );
  }

我从博览会零食中获取了代码:https://snack.expo.io/@trinet/sharing 任何帮助将不胜感激。我真的很困

0 个答案:

没有答案