Validating a downloaded ContentType file in ASP.NET

I store some files in my database, and since I store them in binary format and do not store any other information, I have to make sure that they are all in the same format so that I can "service" them later (if there is an easy way to output file type from an array of bytes, tell me, but this is not the focus here).

So what I need to do is check each downloaded file to make sure it is in the right format.

I installed FieldTemplate with the FileUpload control and CustomValidator:

<asp:FileUpload ID="FileUpload" runat="server" />&nbsp;


<asp:CustomValidator
    ID="CustomValidator1"
    runat="server"
    ErrorMessage="PDF only."
    ControlToValidate="FileUpload"
    OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

What I am missing is the code that should be placed in this CustomValidator1_ServerValidate method, which checks the downloaded file to make sure it is in the right format (in this case, PDF).

.

+3
4

FileUpload.PostedFile.ContentType MIME ( application/pdf). (.pdf). -, MIME- () .

+4

. . TXT , pdf . "application\pdf".

, . PDF , 3 . 3 , . : 37, 80, 68.

, (InputFile1.FileContent.ReadByte()), 3 , , PDF. - , script IIS. , .

+4

FileUpload.PostedFile.ContentType , .

- , : , MIME PDF "application/pdf" "text/pdf", .

+2

User can trick him. In the solution above there is no verification of the actual contents of the bytes. I can send you an executable file and disguise it as a PDF, and this will not catch it.

+2
source

All Articles