【问题标题】:React Native Image File to base64 / blob将本机图像文件反应为 base64 / blob
【发布时间】:2021-09-04 22:44:50
【问题描述】:

这是我的 react 原生版本 反应原生 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,也没有成功,请帮忙

【问题讨论】:

    标签: image react-native base64 expo


    【解决方案1】:

    创建一个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 请求

    【讨论】:

    • 感谢您的帮助,但我不认为是这样,因为它只返回类似这样的内容 FormData { "_parts": Array [ Array [ "file", Object { "name": " SmapleIMage.jpg", "type": "image.jpg", "uri": "file:///storage/emulated/0/DCIM/e0c4c18c-eee2-4965-8b38-368eae22e98a.jpg", }, ], ], } .................. 我需要的是 blob 文件,这样我就可以在其他任何地方看到该图像,只需将 jpg 转换为 64 并将 64 转换为 jpg 当用户想要查看它时跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多