开发人员,我正在使用react-native-image-picker将图像上传到Nodejs服务器和MongoDB,当我在react native应用程序上选择图像时,它在控制台日志中显示图像fileName =“ null”。我苦苦挣扎了三个星期,找不到任何解决方案。下面我发布了console.log结果:
Response = {"fileName": null, "fileSize": 13712705, "height": 3024, "isVertical": false, "origURL": "assets-library://asset/asset.HEIC?id=CC95F08C-88C3-4012-9D6D-64A413D254B3&ext=HEIC", "type": "image/jpeg", "uri": "file:///Users/shinobi/Library/Developer/CoreSimulator/Devices/9DBEA6D8-101E-4B9C-9DB0-D1CABA724AAF/data/Containers/Data/Application/44C0E455-2A43-40A3-A2EE-213A39B7743C/tmp/35A90028-55FA-4218-8FB7-34EB1DE62F58.jpg", "width": 4032}
下面是我的react-native-image-picker代码:
const selectImage = async () => {
const options = {
noData: true,
};
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 if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const source = {uri: response.uri};
// You can also display the image using data:
// const source = { uri: 'data:image/jpeg;base64,' + response.data };
setImage(source);
}
});
};
您仅保存图像的URI,以便访问名称和其他数据,因此必须保存“响应”对象
尝试
const source = {fileName: response.fileName, uri: response.uri};
// You can also display the image using data:
// const source = { uri: 'data:image/jpeg;base64,' + response.data };
setImage(source);