如何从flutter中的base64字符串获取文件扩展名|镖

发布于 2025-01-23 05:17:35 字数 139 浏览 5 评论 0 原文

我有一个来自API的文档的base64字符串。我想知道是哪种扩展/文件格式。因为如果它在jpg/jpeg/png中,我想在图像小部件中显示它。或者,如果是PDF格式,我想在PDFView窗口小部件中显示。因此,有什么方法可以从base64获取文件扩展名。有包裹吗?

I have a base64 string of a document from api. I want to know which extension/file format is that. Because if it is in jpg/jpeg/png i want to show it in image widget. Or if it is in pdf format i want to show it in PdfView widget. So is there any way to get file extension from base64. Is there any package for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2025-01-30 05:17:35

如果您有base64字符串,则可以通过检查base64字符串的第一个字符来检测文件类型:

'/'表示JPEG。

“我”的意思是png。

'r'表示gif。

'u'表示WebP。

'J'表示PDF。

我为此写了一个功能:

String getBase64FileExtension(String base64String) {
    switch (base64String.characters.first) {
      case '/':
        return 'jpeg';
      case 'i':
        return 'png';
      case 'R':
        return 'gif';
      case 'U':
        return 'webp';
      case 'J':
        return 'pdf';
      default:
        return 'unknown';
    }
  }

If you have a base64 string you can detect file type by checking the first character of your base64 string:

'/' means jpeg.

'i' means png.

'R' means gif.

'U' means webp.

'J' means PDF.

I wrote a function for that:

String getBase64FileExtension(String base64String) {
    switch (base64String.characters.first) {
      case '/':
        return 'jpeg';
      case 'i':
        return 'png';
      case 'R':
        return 'gif';
      case 'U':
        return 'webp';
      case 'J':
        return 'pdf';
      default:
        return 'unknown';
    }
  }
来世叙缘 2025-01-30 05:17:35

如果您没有原始文件名,则无法恢复它。那是元数据不是文件内容的一部分,而base64编码仅在文件的内容上运行。最好保存原始文件名,最好。

如果不能,则可以使用 package:mime 猜猜来自少量二进制数据的文件的MIME类型。您可以从base64字符串中解码第一个 n ×4个字符(有效的base64字符串必须具有4个倍数的长度),解码它,然后调用 lookupmimetype

软件包:Mime 具有 您可以动态计算 n 的值:

import 'dart:convert';
import 'package:mime/mime.dart' as mime;

String? guessMimeTypeFromBase64(String base64String) {
  // Compute the minimum length of the base64 string we need to decode
  // [mime.defaultMagicNumbersMaxLength] bytes.  base64 encodes 3 bytes of
  // binary data to 4 characters.  
  var minimumBase64Length = (mime.defaultMagicNumbersMaxLength / 3).ceil() * 4;
  return mime.lookupMimeType(
    '',
    headerBytes: base64.decode(base64String.substring(0, minimumBase64Length)),
  );
}

对于 package:mime 支持写作时的类型, mime.defaultmagicnumbersmaxlength 为12(这转化为需要从base64字符串中解码前16个字节)。

If you don't have the original filename, there's no way to recover it. That's metadata that's not part of the file's content, and base64 encoding operates only on the file's content. It'd be best if you could save the original filename.

If you can't, you can use package:mime to guess the MIME type of the file from a small amount of binary data. You could decode the first n×4 characters from the base64 string (a valid base64 string must have a length that's a multiple of 4), decode it, and call lookupMimeType.

package:mime has a defaultMagicNumbersMaxLength value that you can use to compute n dynamically:

import 'dart:convert';
import 'package:mime/mime.dart' as mime;

String? guessMimeTypeFromBase64(String base64String) {
  // Compute the minimum length of the base64 string we need to decode
  // [mime.defaultMagicNumbersMaxLength] bytes.  base64 encodes 3 bytes of
  // binary data to 4 characters.  
  var minimumBase64Length = (mime.defaultMagicNumbersMaxLength / 3).ceil() * 4;
  return mime.lookupMimeType(
    '',
    headerBytes: base64.decode(base64String.substring(0, minimumBase64Length)),
  );
}

For the types that package:mime supports as of writing, mime.defaultMagicNumbersMaxLength is 12 (which translates to needing to decode the first 16 bytes from the base64 string).

少钕鈤記 2025-01-30 05:17:35

一种解决方案是使用魔法数字从base64检测到文件类型:

import 'dart:convert';
import 'dart:typed_data';

String getFileExtensionFromBase64(String base64String) {
  // Decode base64 to get the raw bytes
  Uint8List bytes = base64Decode(base64String);

  // Check the magic numbers to determine the file type
  if (bytes.startsWith([0xFF, 0xD8])) {
    return 'jpg'; // JPEG file
  } else if (bytes.startsWith([0x89, 0x50, 0x4E, 0x47])) {
    return 'png'; // PNG file
  } else if (bytes.startsWith([0x25, 0x50, 0x44, 0x46])) {
    return 'pdf'; // PDF file
  } else if (bytes.startsWith([0x00, 0x00, 0x00, 0x18]) || bytes.startsWith([0x00, 0x00, 0x00, 0x20])) {
    return 'mp4'; // MP4 video
  } else if (bytes.startsWith([0x1A, 0x45, 0xDF, 0xA3])) {
    return 'mkv'; // MKV video
  } else if (bytes.startsWith([0x49, 0x44, 0x33])) {
    return 'mp3'; // MP3 audio
  } else if (bytes.startsWith([0x4F, 0x67, 0x67, 0x53])) {
    return 'ogg'; // OGG audio
  } else {
    return 'unknown'; // Could not identify the file
  }
}

// Helper function to check if the bytes start with a specific sequence
extension Uint8ListExtensions on Uint8List {
  bool startsWith(List<int> pattern) {
    if (pattern.length > this.length) return false;
    for (int i = 0; i < pattern.length; i++) {
      if (this[i] != pattern[i]) return false;
    }
    return true;
  }
}

One solution would be detect File Type from Base64 Using Magic Numbers:

import 'dart:convert';
import 'dart:typed_data';

String getFileExtensionFromBase64(String base64String) {
  // Decode base64 to get the raw bytes
  Uint8List bytes = base64Decode(base64String);

  // Check the magic numbers to determine the file type
  if (bytes.startsWith([0xFF, 0xD8])) {
    return 'jpg'; // JPEG file
  } else if (bytes.startsWith([0x89, 0x50, 0x4E, 0x47])) {
    return 'png'; // PNG file
  } else if (bytes.startsWith([0x25, 0x50, 0x44, 0x46])) {
    return 'pdf'; // PDF file
  } else if (bytes.startsWith([0x00, 0x00, 0x00, 0x18]) || bytes.startsWith([0x00, 0x00, 0x00, 0x20])) {
    return 'mp4'; // MP4 video
  } else if (bytes.startsWith([0x1A, 0x45, 0xDF, 0xA3])) {
    return 'mkv'; // MKV video
  } else if (bytes.startsWith([0x49, 0x44, 0x33])) {
    return 'mp3'; // MP3 audio
  } else if (bytes.startsWith([0x4F, 0x67, 0x67, 0x53])) {
    return 'ogg'; // OGG audio
  } else {
    return 'unknown'; // Could not identify the file
  }
}

// Helper function to check if the bytes start with a specific sequence
extension Uint8ListExtensions on Uint8List {
  bool startsWith(List<int> pattern) {
    if (pattern.length > this.length) return false;
    for (int i = 0; i < pattern.length; i++) {
      if (this[i] != pattern[i]) return false;
    }
    return true;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文