从React Native上传图像文件到Django



那么在React Native端console。log正在打印这个图像文件就像我在做console.log(photos[1])并得到这个

{"fileName": "rn_image_picker_lib_temp_0d752f09-0fab-40f0-8c89-13dec3695de0.jpg", "fileSize": 374466, "height": 810, "type": "image/jpeg", "uri": "file:///data/user/0/com.cheersfe/c
ache/rn_image_picker_lib_temp_0d752f09-0fab-40f0-8c89-13dec3695de0.jpg", "width": 1000}

这是我的React Native Code

const createFormData = (photos, body = {}) => {
const data = new FormData();
if (photos.length > 1) {
console.log(photos[1])
data.append("images", photos[1]);
}
Object.keys(body).forEach((key) => {
data.append(key, body[key]);
});
return data;
};
const onAddButtonClick = () => {
if (value === null) {
Alert.alert("Hold On!", "Please select a goal");
} else if (descriptionText === "") {
Alert.alert("Hold On!", "Update description required");
}
setLoading(true);
const body = createFormData(photos, { join_goal: value, body: descriptionText });
postGoalUpdatePost(body).then(response => {
// navigation.navigate("PostsScreen");
setLoading(false);
}).catch(error => {
const errorMessage = `${error.name}: ${error.message}`;
});
setLoading(false);
};
export async function postGoalUpdatePost(body) {
return constructConfig().then(config => {
return axios.post(`${url}/posts/update`, body, config).then(response => {
return response.data;
});
});
}

但是在Django端,当我看request.data['images']时,我得到这个疯狂的十六进制字符串:

x7f.x�������uJr|�����a=e��|�x00��^�=J0�^������x08x1a\x02;�H�a�a�x19=�u�Kx01�x䞧�x0e�{{�x07x14���}H�2x06?.)%�x7fx1f�x0c�RvI������=H-���[��䌏ÿ^��x00玵�$��<g�x1d}�x1d�x00®7�x1e���Yrx7f�x1f���+&��wт�I�?������$�Ӯz���N?Z�+�x07�Oox7f�?����x7fx13���ܒ1����r����V=J0M�x7f5�I5�ϹVG���x189��`�x7fJ�$��u�1���{x7f.�չ:7���kuO��x15���m,����{x18x�x7f.]�x7f�W��s�x0e=��1�x00֬�e^��0y$sӧ�x0ez՛���x7f3YSx13��|��+�x7fx13��#���;>�k�o�x7fñx1c�x03�x0fx18�;s�'��ߥx19$$�{���O���SI�x1b�Ux1f�x0f�O�x15���}^����֥x08���u���x002��|����|���Y�$�x003�^�A�9�>�j��x1f����zx1f��U��x17�u=zx10\��O����b9�Vx18x04px08������Vd�Gx1f��2O�V��x1f�ک\u?C��qԛJI[���zT"����_�W+�x02x06x0f$u����x00����x15x19�x1f��4Wx0b��z$w���'

显然是作为字符串而不是文件传入的。我该如何解决这个问题?

问题是上传库保存json的格式

{"fileName": "rn_image_picker_lib_temp_0d752f09-0fab-40f0-8c89-13dec3695de0.jpg", "fileSize": 374466, "height": 810, "type": "image/jpeg", "uri": "file:///data/user/0/com.cheersfe/c
ache/rn_image_picker_lib_temp_0d752f09-0fab-40f0-8c89-13dec3695de0.jpg", "width": 1000}

必须在

{'name': file_name, 'uri': uri, 'type': type}

格式。然后需要'content-type'为'multipart/form-data'

相关内容

  • 没有找到相关文章

最新更新