使用api的下载链接下载文件

时间:2019-08-05 02:52:12

标签: android reactjs api react-native

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

我使用let attach = member && JSON.parse(member.attachment);然后  Linking.openURL('http://lkcfesnotification.000webhostapp.com/storage/'+附件[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

  },
});

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

0 个答案:

没有答案