【问题标题】:Convert Image FILE_URI into Base64 string?将图像 FILE_URI 转换为 Base64 字符串?
【发布时间】:2021-10-28 16:41:57
【问题描述】:

我需要将来自图像选择器的文件 Uri 响应转换为 base64 字符串。

我已经尝试使用文件阅读器将文件 uri 转换为 base64,但它不起作用。

const options: ImagePickerOptions = {
      maximumImagesCount: 1,
      width: 1200,
      height: 1200,
      quality: 80
    };

    this.imagePicker.getPictures(options).then(async (results) => {
      const fileData: string = results[0];
            const path: string = fileData.substring(0, fileData.lastIndexOf('/') + 1);
            const fileName: string = fileData.split('/').pop();

            console.log(fileName, "fileName");
            console.log(path, "Path");

            this.file.readAsDataURL(path, fileName)
              .then((base64File) => {
                console.log("here is encoded image ", base64File)
              })
              .catch((err) => {
                console.log('Error reading file', err);
              })
});

请让我知道我哪里出错了。感谢任何建议或解决方案。 提前致谢。

【问题讨论】:

    标签: angular image ionic-framework base64


    【解决方案1】:

    你可以将ImagePickerOptions中的outputType设置为window.imagePicker.OutputType.BASE64_STRING,那么results应该是一个base64字符串数组。 但是您将无法按照您的方式获取文件名。

    const options: ImagePickerOptions = {
      maximumImagesCount: 1,
      width: 1200,
      height: 1200,
      quality: 80,
      outputType: window.imagePicker.OutputType.BASE64_STRING
    };
    
    this.imagePicker.getPictures(options).then(async (results) => {
      const base64File: string = results[0];
      console.log("here is encoded image ", base64File);
    });
    

    您可能必须将 window 转换为 any。

    【讨论】:

    • 嘿!谢谢你快速的回复!但我没有得到回应。我得到 FILE_URI 盯着 file:/// 。如何将该结果转换为base64?如果您能提供帮助,我们将不胜感激。谢谢。
    • 我对 Ionic 不够熟悉,它似乎在浏览器中运行良好。我将用另一种方式更新我的答案。
    猜你喜欢
    • 2018-09-07
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多