In C#, how can I know the file type from a byte[]?

后端 未结 10 1260
不思量自难忘°
不思量自难忘° 2020-11-28 08:28

I have a byte array filled from a file uploaded. But, in another part of the code, I need to know this file type uploaded from the byte[] so I can render the correct content

10条回答
  •  星月不相逢
    2020-11-28 09:04

    Short answer: you can't

    Longer answer: Usually, programs use the file extension to know what type of file they're dealing with. If you don't have that extension, you can only make guesses... for instance, you could look at the first few bytes and check if you recognize a well-known header (XML declaration tag for instance, or bitmap or JPEG header). But that will always be a guess in the end : without some metadata or information about the content, an array of bytes is just meaningless...

提交回复
热议问题