пятница, 25 декабря 2020 г.

Upload files to django rest framework using react native

Uploading file using api in django rest framework. PythonAnywhere
https://youtu.be/BLN3EPjxQ_E



App.js
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
} from 'react-native';
 
export default class MyApp extends Component {
 
 
  upload_image_to_django (){
 
    PicturePath = "file:///storage/emulated/0/Download/myfoto.jpg";
    URLpath = "http://manas93.pythonanywhere.com/file/upload/";
 
    var formData = new FormData();
    formData.append("file", {uri: PicturePath, name: 'myfoto.jpg', type: 'image/jpg'});
    formData.append("remark", "Hello");
 
    fetch( URLpath, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'multipart/form-data',
      },
      body: formData
     })
    .then((responseJson) => {
      console.log(responseJson);
    })
    .catch(error => {
      console.log(error);
    })
 
  }
 
  render() {
    return (
      <View style={styles.content}>
      <Button       
        title="My Button"
        onPress={this.upload_image_to_django}
      />
      </View>
    );
  }
 
}
 
var styles = StyleSheet.create({
    content:{
        flex:1,
        flexDirection:'row',
        alignItems:'center',
        justifyContent:'center'
    }
});

Комментариев нет:

Отправить комментарий