1

这是我从本地手机存储中获取的网址

"file:///storage/emulated/0/Pictures/1548694153266.jpg"

我是否真的必须将其转换为 base64 才能上传到 s3。如果是这样,是否有任何好的库可以将本地 url 从 react native 转换为 base64

为什么将其转换为 base64 很重要?

最好的方法是什么。

我可以通过邮递员轻松地从本地计算机发送图像。从邮递员我可以直接从计算机中选择图像作为图像对象。但在这种情况下,网址不是图像对象?

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);
      })
}; 
4

2 回答 2

0

您可以通过以下步骤执行此操作:

第 1 步: 安装此软件包以将其文件 url 转换为 base64。这是一个好的库的链接,用于将本地 url 从 react native 转换为 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
  }
})

在这里您可以找到为什么将其转换为 base64 很重要

于 2019-03-25T11:12:35.300 回答
0

我正在使用react-native-image-picker这个库,它为您提供 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)
  })

}
于 2019-03-25T08:40:57.753 回答