首页 > 解决方案 > 使用来自 api 的下载链接下载文件

问题描述

我有一个开发网站,我可以在其中上传单个文件和多个文件,这给了我下载链接。我还制作和 api 来获取我的 id。(http://lkcfesnotification.000webhostapp.com/api/notifications)。从 api 我可以使用 JSON.parse 获取图像,但是当谈到我的附件(文件)时,我想成为从浏览器下载的超链接。从 api 我确定存储在我的存储中的路径。并且存储也有我的图像,我可以查看我的图像我该怎么做?

我使用 let attachment = member && JSON.parse(member.attachment); 然后 Linking.openURL(' http://lkcfesnotification.000webhostapp.com/storage/ ' + attachment[0]) } >单击此处下载文件,但找不到此显示页面

import React, { Component } from 'react';
import {
  Alert,
  Image,
  StyleSheet,
  ScrollView,
  View,
  Text,
  Linking,
} from 'react-native';
import {
  InputWithLabel
} from './UI';
import { FloatingAction } from 'react-native-floating-action';

type Props = {};
export default class ShowScreen extends Component<Props> {
  static navigationOptions = ({navigation}) => {
    return {
      title: navigation.getParam('headerTitle')
    };
  };

  constructor(props) {
    super(props)

    this.state = {
      id: this.props.navigation.getParam('id'),
      member: '',
    };

    this._load = this._load.bind(this);
  }

  componentDidMount() {
    this._load();
  }

  _load() {
    let url = 'http://lkcfesnotification.000webhostapp.com/api/notifications/' + this.state.id;

    fetch(url)
    .then((response) => {
      if(!response.ok) {
        Alert.alert('Error', response.status.toString());
        throw Error('Error ' + response.status);
      }

      return response.json()
    })
    .then((member) => {
      this.setState({member});
    })
    .catch((error) => {
      console.error(error);
    });
  }

  render() {
    let member = this.state.member;
   // let af = 'http://lkcfesnotification.000webhostapp.com/storage/';
    console.log(member);
    console.log(member.image);
    let image = member && JSON.parse(member.image);
    let attachment = member && JSON.parse(member.attachment);
    return (
      <View style={styles.container}>
        <ScrollView>
          <InputWithLabel style={styles.output}
            label={'Title'}
            value={member ? member.title : ''}
            orientation={'vertical'}
            multiline={true}
            editable={false}
          />
          <InputWithLabel style={styles.output}
            label={'Department'}
            value={member ? member.department : ''}
            orientation={'vertical'}
            editable={false}
          />
          <InputWithLabel style={styles.output}
            label={'Publish'}
            value={member ? member.updated_at : ''}
            orientation={'vertical'}
            editable={false}
          />
          <InputWithLabel style={[styles.output, {height: 600, textAlignVertical: 'top'}]}
            label={'Description'}
            value={member ? member.description : ''}
            orientation={'vertical'}
            editable={false}
            multiline={true}
          />          
          <Text>
          {image && image.length && image.map(image => {
      return <Image 
            source={{uri: 'http://lkcfesnotification.000webhostapp.com/storage/' + image}}
            style={{width: 300, height: 300}}
            orientation={'vertical'}
          />  
   })
}
</Text>
<Text style={styles.TextStyle} onPress={ ()=> Linking.openURL('http://lkcfesnotification.000webhostapp.com/storage/' + attachment[0]) } >Click here for to Download file</Text>  
<Text style={styles.TextStyle} onPress={ ()=> Linking.openURL(member.link) } >Click here for More Detail</Text>   
        </ScrollView>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    backgroundColor: '#fff',
  },
  output: {
    fontSize: 24,
    color: '#000099',
    marginTop: 10,
    marginBottom: 10,
  },
    TextStyle: {

    color: '#E91E63',
    textDecorationLine: 'underline',
    fontSize: 30

  },
});

我想按超链接直接连接到设备的默认浏览器并下载文件

标签: androidreactjsapireact-native

解决方案


推荐阅读