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

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

    You don't want to do it that way. Call Path.GetExtension when the file is uploaded, and pass the extension around with the byte[].

    0 讨论(0)
  • 2020-11-28 09:28

    Not sure, but maybe you should investigate about magic numbers.

    Update: Reading about it, I don't think it's very reliable though.

    0 讨论(0)
  • 2020-11-28 09:28

    Reminds me of back in the day we, er um "some people" used to share 50MB rar files on the early free image hosting sites, by just adding the .gif extension to the .rar filename.

    Clearly if you are public facing and your are expecting a certain file type, and you have to be sure it is that file type, then you can't just trust the extension.

    On the other hand, if your app would have no reason to distrust the the uploaded extension and or MIME type, then just get those when the file is uploaded like the answers you received from @rossfabircant and @RandolphPotter. create a type that has the byte[], as well as the original extension or mimetype, and pass that around.

    If you need to verify that the file is actually a certain expected type like a valid .jpeg, or .png you can try to interpret the file as those types and see if it opens successfully. (System.Drawing.Imaging.ImageFormat)

    If you are trying to classify the file only from the binary contents, and it could be any format in the whole wide world, that is really a tough, open-ended problem and there is no 100% reliable way to do it. You could invoke TrID against it, and there are likely similar forensics tools used by law enforcement investigators if you can find (and afford) them.

    If you don't have to do it the hard way, don't.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题