react-native下载文件链接跳转

水麒
2023-12-01

react-native的文章有的是下载链接,不能直接在app打开的,跳转浏览器直接打开链接下载

componentDidMount(){
    let url;
    //收鸽清单同文章显示格式不同
    if(this.props.navigation.state.params.sn.is_file!=1){
        url='http://'+articleIp+'/showArticle/'+this.props.navigation.state.params.sn.article_sn;
    }else{
        url='http://'+articleIp+this.props.navigation.state.params.sn.memo;
        let fileString = this.props.navigation.state.params.sn.memo;
      
        if(fileString.indexOf("html") == "-1"||fileString.indexOf("txt") == "-1"){
            this.setState({
                webViewFlag:true
            });
            Linking.canOpenURL(url).then(supported => {
                if (!supported) {
                    Alert.alert(
                        '提示',
                        "请先下载浏览器",
                        [
                            {text: '确定'},
                        ],
                    );
                } else {
                    return Linking.openURL(url);
                }
            }).catch(err =>
                Alert.alert(
                    '提示',
                    "打开文章链接失败",
                    [
                        {text: '确定'},
                    ],
                ));
        }
    }
    this.setState({
        url:url
    });
}

 

 类似资料: