【发布时间】:2018-04-07 16:14:15
【问题描述】:
我正在使用 Expo 的图像选择器,我得到了这个输出:
Object {
"cancelled": false,
"height": 468,
"uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540jbaek7023%252Fstylee/ImagePicker/9a12f0a3-9260-416c-98a6-51911c91ddf4.jpg",
"width": 468,
}
我可以渲染我的图像,但我刚刚意识到 URL 是 手机的本地 URI。
我正在使用 Redux-Thunk 和 Axios 发送 HTTP POST 请求:
export const sendPost = ( imageUri, title, content ) => async dispatch => {
let response = await axios.post(`${ROOT_URL}/rest-auth/registration/`, {
image, <<<<- I can't put image uri here :( it's LOCAL path
title,
content
})
if(response.data) {
dispatch({ type: POST_CREATE_SUCCESS, payload: response.data.token })
} else {
dispatch({ type: POST_CREATE_FAIL })
}
}
更新我更改了请求调用
let headers = { 'Authorization': `JWT ${token}`};
if(hType==1) {
headers = { 'Authorization': `JWT ${token}`};
} else if (hType==2) {
headers = { 'Authorization': `Bearer ${token}`};
}
let imageData = new FormData();
imageData.append('file', { uri: image });
let response = await axios.post(`${ROOT_URL}/clothes/create/`, {
image: imageData,
text, bigType, onlyMe ...
}, {headers});
!!对不起,但图像变量名称的复杂性; image 是图像的uri。我不想更改原始变量名称的名称
在服务器上,它正在打印
'image': {'_parts': [['file', {'uri': 'file:///data/user/0/host.exp.exponent
/cache/ExperienceData/%2540jbaek7023%252Fstylee/
ImagePicker/78f7526a-1dfa-4fc9-b4d7-2362964ab10d.jpg'}]]}
我发现 gzip 压缩是一种发送图像数据的方法。有帮助吗?
【问题讨论】:
-
这个答案可能对你有帮助stackoverflow.com/a/34972537/3514603
-
我正在调查。但我认为 React 比 vanila js 有更好的选择
-
React 只是一个 UI 库。不多也不少。并且您想使用 XHR。如果您的后端在该端点支持此格式,您可以使用
FormData上传图片。 -
@RadovanKuka 我想我必须关注你的情况。你能给我发一个答案吗?
-
你是什么意思?我按照其他人的建议建议了 FormData,或者您可以将该图像转换为 Base64,如链接问题中所回答的那样。
标签: javascript image reactjs http react-native