首页 > 解决方案 > 反应本机图像上传不适用于 PHP 远程服务器 URL

问题描述

当我将 localhost url 与我的 php 应用程序一起使用时,我能够使用下面给出的反应本机代码上传图像。但是,当我将 php 应用程序托管到服务器时,除了 fileupload 之外,所有其他 API 都在 react native 应用程序中工作。当我调试反应本机应用程序从服务器收到的响应时,它显示如下。

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

下面给出了用于文件上传的 react-native 代码。

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
    });
  }
});

}

我也尝试了以下选项来获取 uri,这在 localhost 中有效,如上面的代码。

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

我在这里想念什么?我是否必须在 ios 清单或其他任何地方添加任何配置或设置才能使其与远程服务器一起使用?

注意:我已经直接用邮递员测试了远程服务器代码,并且图像上传工作正常。一旦托管,它就不仅仅在反应本机应用程序中工作。

标签: phpreactjsreact-nativefile-uploadreact-native-ios

解决方案


推荐阅读