将本机图像文件反应为 base64 / blob

时间:2021-06-21 08:41:37

标签: image react-native base64 expo

这是我的本机版本 反应原生cli:2.0.1 反应原生:0.63.4

有人可以帮忙吗?我尝试了很多方法来将我的文件从移动设备更改为 base64 或 blob 发送到服务器 但是没有人可以将我的文件更改为 base 64 或 blob

这是我的控制台日志:路径:看起来像 file:///storage/emulated/0/DCIM/b01f3aef-4b56-48d9-b7a2-e3424df0f054.jpg --------IMAGEURI

是的,这是一个图像 uri,我可以通过调用将其显示在我的移动屏幕上

<Image uri={that file path...}/>

for (var i = 0 ; i < entriesImage.length ; i++){
        IdImage = [i][0]
        LabelImage = entriesImage[i][0]
        ImageUri = entriesImage[i][1].uri
        Notes = entriesImage[i][1].notes
        Latitude = entriesImage[i][1].location.latitude
        Longitude = entriesImage[i][1].location.longitude
        //DateImage = entriesImage[i][1].date
        DateImage = new Date()
        console.log(ImageUri,'--------IMAGEURI')
        objImage = {
            "surveyId": "3",
            "photo": entriesImage[i][1].uri,
            "photoId": [i][0],
            "label": entriesImage[i][0],
            "latitude": entriesImage[i][1].location.latitude,
            "longitude": entriesImage[i][1].location.longitude,
            "photoDate": DateImage,
            "notes": Notes
        }
        arrayImage.push(objImage)
    }

console.log(ImageUri,'--------IMAGEURI'), ImageUri 作为我想发送到服务器的文件路径

但在如何转换为 base64 或 blob 方面仍然没有运气

我也尝试过 expo image manipulator,也没有运气,请帮忙

1 个答案:

答案 0 :(得分:0)

创建一个 formData 并像这样使用它发送到服务器

创建函数

const CreateFormData = (filePath) => {
  var formdata = new FormData();
  formdata.append('file', {
    name: 'SampleImage.jpg',
    uri: filePath, // File path
    type: 'image/jpg',
  });
  return formdata;
};

现在就这样使用

const response = CreateFormData("file:///storage/emulated/0/DCIM/b01f3aef-4b56-48d9-b7a2-e3424df0f054.jpg")

现在在正文中使用此 response formData 执行 POST 请求