【发布时间】:2021-10-16 22:18:01
【问题描述】:
我使用 react-native-image-crop-picker 创建图像选择器,一切顺利...我从选择器(路径、mime 等)获取数据,但是当我将其上传到服务器时,数据会保留返回null
这是我上传图片的方式:
//this is my state
constructor() {
super();
this.state = {
image: null,
images: null
...
};
}
//this is my insert function
doTambah = async => {
const {
...
image,
} = this.state;
const req = {
...
foto: image,
};
console.log(req);
api
.post('/tambah_produk', req)
.then(res => {
alert('data berhasil disimpan');
})
.catch(err => {
alert(err);
console.log(err);
});
}
//this is my picker function
pickSingle() {
ImagePicker.openPicker({
width: 500,
height: 500,
forceJpg: true,
includeExif: true,
})
.then(image => {
console.log('received image', image);
this.setState({
image: {
uri: image.path,
width: image.width,
height: image.height,
type: image.mime,
size: image.size
},
images: null,
});
})
.catch(e => {
console.log(e);
Alert.alert(e.message ? e.message : e);
});
}
//this is how I input image inside render
render() {
...
const {
...
image,
images
} = this.state;
return (
<View style={styles.container}>
<View style={styles.formWrapper}>
<View style={styles.formRow}>
...
<View style={{alignItems: 'center', marginBottom: hp('1%')}}>
<Text style={{alignSelf: 'flex-start'}}>Tambah Foto</Text>
<ScrollView>
{this.state.image ? this.renderAsset(this.state.image) : null}
{this.state.images
? this.state.images.map(i => (
<View key={i.uri}>{this.renderAsset(i)}</View>
))
: null}
</ScrollView>
</View>
<TouchableOpacity
onPress={() => this.pickSingle()}
style={styles.button}>
<Text style={styles.text}>Upload Thumbnail</Text>
</TouchableOpacity>
</View>
<View style={styles.btnAddContainer}>
<TouchableOpacity
style={styles.btnAdd}
onPress={() => this.doTambah()}>
<Text style={styles.textBtnAdd}>Tambah Barang</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
当我尝试记录图像时,返回数据似乎正确
received image {"height": 1080, "mime": "image/jpeg", "modificationDate": "1628874621000", "path": "file:///data/user/0/com.gapoktanapps/cache/react-native-image-crop-picker/IMG-20210813-WA0034.jpg", "size": 78463, "width": 1000}
但是当我将图像上传到服务器时,我不断收到错误:data: null
为什么我的 data: null ?怎么了 ?以及如何解决?
【问题讨论】:
标签: react-native