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

后端 未结 10 1273
不思量自难忘°
不思量自难忘° 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:29

    If you have a limited number of expected file types you want to support, magic numbers can be the way to go.

    A simple way to check is to just open example files with a text/hex editor, and study the leading bytes to see if there is something there you can use to differentiate/discard files from the supported set.

    If, on the other hand, you are looking to recognize any arbitrary file type, yeah, as everyone has stated already, tough.

提交回复
热议问题