php - 反应本机图像上传不适用于 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 清单或其他任何地方添加任何配置或设置才能使其与远程服务器一起使用?
注意:我已经直接用邮递员测试了远程服务器代码,并且图像上传工作正常。一旦托管,它就不仅仅在反应本机应用程序中工作。
解决方案
推荐阅读
- html - 图像、标题和文本的对齐
- c++ - 性能差异:std::accumulate vs std::inner_product vs Loop
- field - 如何获取选择字段的特定项目。奥多 8
- php - 为什么 php 标签内的内容在 index.php 内有问题
- navision - 时间格式
在 Microsoft Dynamics Navision 2009 中始终为零 - python - Django Rest 框架,如何使用 foreignkey_id 字段包含“__all__”字段和相关字段
- javascript - HTML/Javascript 多次读取单个文件
- c# - 使用 SDK 17134 以 UWP 为目标的 NuGet 包中缺少 PackageReference
- java - Fortnite Tracker API 似乎没有工作,或者我做错了什么?
- c++ - 使用重定向的标准输入处理子进程中的 kbhit