微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试从React Native Webview下载pdf

如何解决尝试从React Native Webview下载pdf

我知道我似乎无法正常工作。已添加“ 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 任何帮助将不胜感激。我真的很困

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。