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?
2 Answers
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';
}
}

- 1,944
- 2
- 11
- 36
-
1I think it's important to understand how this works so that readers are aware of its limitations. While it probably works acceptably if your inputs are constrained to those types, it will be much less reliable for other file types. The first character of the base64 string is derived from only the upper 6 bits of the first byte of the original data. For example, GIF files start with a byte with the ASCII value for `G` (0x47). Any file that starts with `D,` `E`, `F`, or `G` would have a base64 encoding that has the same first character. – jamesdlin Apr 22 '22 at 15:02
-
1This answer is wrong, as long as it is written like this: *"'/' means jpeg."*. You can write that it is likely to be a file of a certain type when there is a certain character on the beginning, but please don't make such highly misleading claims. Base64 encoding does not have such a specification, and beginners might take this for granted. In [my answer here](https://stackoverflow.com/questions/62329321/how-can-i-check-a-base64-string-is-a-filewhat-type-or-not/62330081#62330081) I explained how the headeres of certain file type lead to some typical character sequences in Base64. – jps Apr 24 '22 at 08:30
-
What about document and excel file ? how to find out from base64data – Yalamandarao Jun 16 '22 at 01:20
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).

- 81,374
- 13
- 159
- 204