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

前端 未结 5 1834
天命终不由人
天命终不由人 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 08:04

    Reading the contents of the file is the fool proof way. Since you are building it in .Net, you could probably check the MIME Type of the uploaded file.

    You can DllImport urlmon.dll to help. Please refer a post at: http://coding-passion.blogspot.com/2008/11/validating-file-type.html

    And to clarify regarding Content-type, it invariably is driven by the extension of the file. So even a .zip file got its extension renamed to .txt, the content type will still say Text only.

提交回复
热议问题