从Expo ImagePicker反应本机上传图像到php服务器

时间:2018-09-07 14:09:28

标签: php reactjs image upload native

我使用此功能将图像提交到服务器,但是无法保存图像。请帮助我如何使用PHP:(。

onChooseImage = async () => {
        this.takeAndUploadPhotoAsync()
        .then(function(response) {
            return response.json();
        })
        .then(function(res){
            console.log(res);
        })
        .catch(function(error){
            console.log(error);
        });
}

takeAndUploadPhotoAsync = async () => {
    let result = await ImagePicker.launchImageLibraryAsync();
    if (result.cancelled) {
      return;
    }

    let localUri = result.uri;
    let filename = localUri.split('/').pop();
    let match = /\.(\w+)$/.exec(filename);
    let type = match ? `image/${match[1]}` : `image`;
    let formData = new FormData();
    formData.append('photo', { uri: localUri, name: filename, type });

    var token = await AsyncStorage.getItem('token');
    return await fetch(SERVER_ADDRESS_HERE, {
      method: 'POST',
      body: formData,
      header: {
        'content-type': 'multipart/form-data',
        'Authorization': token,
      },
    });
  }

如果上面的代码不正确或应该更改为更好的代码,请进一步向我解释。谢谢!

0 个答案:

没有答案