dtmu88600 2019-08-12 12:32
浏览 138

React Native Image上传不与PHP远程服务器URL一起使用

I was able to upload the image using the react native code given below when I use the localhost url with my php application. However When I host the php application to the server, all the other API's are working from the react native application except the fileupload. When I debug the response received by react native application from the server, it shows as below.

{"name":"avatar.png","type":"","tmp_name":"","error":1,"size":0}

The react-native code used for the file upload is given below.

upload(){
        const body = new FormData(); 
        body.append('uid', 1);
        body.append('image', {
                uri: this.state.avatarSource.uri,
                type: this.state.avatarSource.type, 
                name: this.state.avatarSource.fileName, 
                size:this.state.avatarSource.fileSize 
            })            
        fetch(config.API_URL+"/upload/image/1", {
            method: 'POST',
            body: body
        })
            .then(res => res.text())
            .then((responseJson) => {
               console.log(responseJson);
            })
            .catch((error) => {
              console.log(error);
            });
}

addPhotos(){
ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', response);
  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else {

    const source = response;

   console.log(source);
    this.setState({
      avatarSource: source
    });
  }
});

}

I have tried the following option as well to get the uri and this works in the localhost as above code.

source = { uri: response.uri.replace('file://', '')}

What am I missing here? Do I have to add any configurations or settings in the ios manifest or anywhere else to get this working with remote server?

NOTE: I have tested the remote server code directly with postman and the image uploading is working. Its not working only in the react native application once its hosted.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安装svn网络有问题怎么办
    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 latex怎么处理论文引理引用参考文献