javascript - 如何使用图像选择器(EXPO)生成的本地 uri 将图像上传到服务器?
问题描述
我正在尝试将图像或文件从移动设备发送到后端,但图像选择器无法为我提供图像提供的 URI 无法识别。
_pickImage = async () => {
let result = await DocumentPicker.getDocumentAsync({});
console.log(result);
if (!result.cancelled) {
this.setState({ files: result.uri });
// upload file && get File Id
axios({
method: "post",
url: "https://someurl",
data: {
image: this.state.file
},
headers: {
"Content-Type": "multipart/form-data",
// TODO: upadte with dynamic token
Authorization:
"Bearer {token}"
}
}).then(
function(response) {
console.log(response.data.message);
// this.setState({
// hospItems: response.data.data
// });
}.bind(this)
);
}
};
控制台.log(结果);返回:: Object { "name": "1554736582840.jpg", "size": 45240, "type": "success", "uri": "file:///data/user/0/host.exp.exponent /cache/ExperienceData/%2540abhinavofficial%252Fmobi-medic/DocumentPicker/cd34bbd8-7c0c-414a-a76d-8d06e2abf670jpg", }
来自服务器的响应:图像字段是必需的。
解决方案
推荐阅读
- java - Java FX 按钮突出显示
- python - 在 jupyter 笔记本上找不到 PhantomJS
- javascript - 如何在 React Hooks 中使用 componentWillMount()?
- laravel - Lumen/Laravel 从 Http Header 获取参数值
- python-3.x - 在数组中创建一组随机数字,而同一数字连续重复三次
- python - 输入 [] 出错:findDistance() 接受 0 个位置参数,但给出了 1 个
- reactjs - 强制在方法中重复变量
- database - 仅出现在功能依赖关系的 RHS 上的属性可以是主要属性吗?
- php - PHP - 我在下载高文件时遇到问题
- react-native - 做某事后如何插入服务器