0

I have following sample base64 string without datauri.

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT...

How do I get the file type/extension in c#?

  • No, I would like to know programmatically to decide the file type. – Diminish Narer Dec 17 '21 at 09:18
  • That's a jpeg.. (`/9j/` is a partial encode of the jpeg header, png b64 start with `iVBOR`... you'll probably have to build some library of prefixes for files that have headers. Get a few examples of every file you're interested in and b64 them, then look at the common N bytes a tthe start. If the file has a consistent header then you'll get some consistent b64 bytes) – Caius Jard Dec 17 '21 at 09:21
  • based on the information you could write a program that decides the file type. Or try to find a library that does this. Not sure what kind of answer you expect. And btw. it's a JPG file in this case. – jps Dec 17 '21 at 09:23
  • Dear @CaiusJard, Do you know what are the start header for **AVI, MOV, FLV, OGG, MP4,AVI**? – Diminish Narer Dec 17 '21 at 09:37
  • usually you find them by searching for "AVI file format" or similar. https://docs.fileformat.com/ has also a lot of descriptions. And https://en.wikipedia.org – jps Dec 17 '21 at 09:46
  • I don't but you can work it out. For example you can go to here: https://www.file-recovery.com/avi-signature-format.htm and see an AVI is like `RIFFxxxxAVI LIST` where the xxxx varies, then you can go to https://www.base64encode.org/ and type `RIFFxxxxAVI LIST` in there and see it encodes to `UklGRlpaWlpBVkkgTElTVA==` whereas `RIFFyyyyAVI LIST` encodes to `UklGRnl5eXlBVkkgTElTVA==` so you have `UklGR ... lBVkkgTElTVA..` ... as common between the two and could form part of your check (other files such as WAV start with RIFF too so it's not enough to just check that) – Caius Jard Dec 17 '21 at 09:49
  • Dear JPS & CaiusJard, Thank you both for taking the time to discuss . I will try it. – Diminish Narer Dec 17 '21 at 09:57
  • 1
    @CaiusJard *other files such as WAV start with RIFF too so it's not enough to just check that* - that's an interesting and important point, I'll improve my linked answer with that information. – jps Dec 17 '21 at 10:04

0 Answers0