How to check the content of an uploaded file without relying on its extension?

前端 未结 5 1832
天命终不由人
天命终不由人 2021-01-06 07:08

How do you go about verifying the type of an uploaded file reliably without using the extension? I\'m guessing that you have to examine the header / read some of the bytes,

5条回答
  •  礼貌的吻别
    2021-01-06 07:45

    The first few bytes of a file will often tell you the file type. See, for example,
    http://www.garykessler.net/library/file_sigs.html
    http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html

    Use System.IO to read the byes as binary after the upload.

    I'm curious, though, why you can't rely on on the ContentType header?

提交回复
热议问题