React native 在发送到服务器并使用multer上传到S3之前,我是否已将本地电话图像url转换为base64?
这是我从本地电话存储器中获取的urlReact native 在发送到服务器并使用multer上传到S3之前,我是否已将本地电话图像url转换为base64?,react-native,multer-s3,React Native,Multer S3,这是我从本地电话存储器中获取的url "file:///storage/emulated/0/Pictures/1548694153266.jpg" 我真的需要将其转换为base64才能上传到s3吗。 如果是这样的话,有没有好的库可以用来转换本地url 对base64进行本机响应 _convertImageToBaseSixFour() { ImgToBase64.getBase64String('YOUR_IMAGE_PATH') // path to your image fr
"file:///storage/emulated/0/Pictures/1548694153266.jpg"
我真的需要将其转换为base64才能上传到s3吗。
如果是这样的话,有没有好的库可以用来转换本地url
对base64进行本机响应
_convertImageToBaseSixFour() {
ImgToBase64.getBase64String('YOUR_IMAGE_PATH') // path to your image from local storage
.then((base64String) => {
// Do your stuff with base64String
})
.catch(err => Alert.alert('Error' + err));
}
为什么将其转换为base64很重要
最好的办法是什么
我可以通过邮递员轻松地从本地计算机发送图像。从邮递员我可以选择图像直接从计算机作为图像对象。但在这种情况下,url不是图像对象
exports.uploadProduct = async (req, res) => {
const uploads = await uploadMulti();
uploads(req, res, function (err) {
let imageFiles = req.files;
const productImages = new Array();
for (var i = 0; i < imageFiles.length; i++) {
fileLocation = imageFiles[i].location;
productImages.push(fileLocation)
}
console.log(productImages);
})
};
我正在使用这个库,它为您提供base64字符串和本地URL,这取决于您选择最适合您的方法
const options = {
title: 'Select Avatar',
storageOptions: {
skipBackup: true,
path: 'images'
},
mediaType: 'photo',
quality: 0.01
}
const pickImage = onUri => {
ImagePicker.showImagePicker(options, response => {
let fileName = response.fileName
if (
Platform.OS === 'ios' &&
(fileName.endsWith('.heic') || fileName.endsWith('.HEIC'))
) {
fileName = `${fileName.split('.')[0]}.JPG`
}
let source = { uri: response.uri, fileName }
const file = {
uri: response.uri,
name: response.fileName,
type: 'image/jpeg'
}
console.log('File Object ', file)
// var source = { uri: 'data:image/jpeg;base64,' + response.data, isStatic: true };
onUri(response.data, response.uri, response.type, fileName, file)
})
}
我正在使用这个库,它为您提供base64字符串和本地URL,这取决于您选择最适合您的方法
const options = {
title: 'Select Avatar',
storageOptions: {
skipBackup: true,
path: 'images'
},
mediaType: 'photo',
quality: 0.01
}
const pickImage = onUri => {
ImagePicker.showImagePicker(options, response => {
let fileName = response.fileName
if (
Platform.OS === 'ios' &&
(fileName.endsWith('.heic') || fileName.endsWith('.HEIC'))
) {
fileName = `${fileName.split('.')[0]}.JPG`
}
let source = { uri: response.uri, fileName }
const file = {
uri: response.uri,
name: response.fileName,
type: 'image/jpeg'
}
console.log('File Object ', file)
// var source = { uri: 'data:image/jpeg;base64,' + response.data, isStatic: true };
onUri(response.data, response.uri, response.type, fileName, file)
})
}
可以通过以下步骤执行此操作: 步骤1:安装此包以将其文件url转换为base64。这里是一个链接 步骤2:然后在代码中
import ImgToBase64 from 'react-native-image-base64';
步骤3:制作一个函数将图像转换为base64
_convertImageToBaseSixFour() {
ImgToBase64.getBase64String('YOUR_IMAGE_PATH') // path to your image from local storage
.then((base64String) => {
// Do your stuff with base64String
})
.catch(err => Alert.alert('Error' + err));
}
步骤4:按下按钮调用此函数。你可以这样做
// *** BUTTON *** //
<View style={styles.buttonContainer}>
<Button onPress={this._convertImageToBaseSixFour} title="Change to Base" color="#FFFFFF" accessibilityLabel="Tap"/> //Call on press
</View>
// *** STYLESHEET for Button *** //
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF'
},
buttonContainer: {
backgroundColor: '#2E9298',
borderRadius: 10,
padding: 10,
shadowColor: '#000000',
shadowOffset: {
width: 0,
height: 3
},
shadowRadius: 10,
shadowOpacity: 0.25
}
})
在这里你可以找到
可以通过以下步骤执行此操作: 步骤1:安装此包以将其文件url转换为base64。这里是一个链接 步骤2:然后在代码中
import ImgToBase64 from 'react-native-image-base64';
步骤3:制作一个函数将图像转换为base64
_convertImageToBaseSixFour() {
ImgToBase64.getBase64String('YOUR_IMAGE_PATH') // path to your image from local storage
.then((base64String) => {
// Do your stuff with base64String
})
.catch(err => Alert.alert('Error' + err));
}
步骤4:按下按钮调用此函数。你可以这样做
// *** BUTTON *** //
<View style={styles.buttonContainer}>
<Button onPress={this._convertImageToBaseSixFour} title="Change to Base" color="#FFFFFF" accessibilityLabel="Tap"/> //Call on press
</View>
// *** STYLESHEET for Button *** //
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF'
},
buttonContainer: {
backgroundColor: '#2E9298',
borderRadius: 10,
padding: 10,
shadowColor: '#000000',
shadowOffset: {
width: 0,
height: 3
},
shadowRadius: 10,
shadowOpacity: 0.25
}
})
在这里你可以找到
请检查下面的答案,希望有帮助。请检查下面的答案,希望有帮助。先生,我可以再澄清一个问题吗?我使用的是一个不同的图像库,它只给我文件的本地url:所以我想做的是通过json将这些图像url上传到s3。我正在尝试将这些图像url发送到动作请求体中的s3。{productImages:[图像:file:///storage/emulated/0/Pictures/1548694153266.jpg,图片:file:///storage/emulated/0/Pictures/1548694153266.jpg ] }. 使用PostMan,我可以将这些作为表单数据发送。选择“本地图像”并将其删除works@bee321这可能会有帮助:先生,我能再澄清一个问题吗?我使用的是一个不同的图像库,它只给我文件的本地url:所以我想做的是通过json将这些图像url上传到s3。我正在尝试将这些图像url发送到动作请求体中的s3。{productImages:[图像:file:///storage/emulated/0/Pictures/1548694153266.jpg,图片:file:///storage/emulated/0/Pictures/1548694153266.jpg ] }. 使用PostMan,我可以将这些作为表单数据发送。选择“本地图像”并将其删除works@bee321这可能有助于结帐: