Click here to Skip to main content
15,907,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If i upload a file test.exe, then my application restricts it and give an alert message.
But, if i rename the file as test.exe.txt the application uploads it in database successfully.

How to restrict the upload for test.exe.txt or test.zip.txt?

Please suggest.

Thanks,
Sneha.

What I have tried:

I have tried restricting the contenttype of a file but it is not working as required.

for ex:

VB
If (fuAttachment.PostedFile.ContentType <> "text/HTML" And fuAttachment.PostedFile.ContentType <> "text/plain" And fuAttachment.PostedFile.ContentType <> "Application/msword" And fuAttachment.PostedFile.ContentType <> "image/jpeg" And fuAttachment.PostedFile.ContentType <> "image/GIF" And fuAttachment.PostedFile.ContentType <> "application/pdf" And fuAttachment.PostedFile.ContentType <> "Application/x-msexcel" And fuAttachment.PostedFile.ContentType <> "image/bmp" And fuAttachment.PostedFile.ContentType <> "application/vnd.ms-outlook" And fuAttachment.PostedFile.ContentType <> "image/tiff" And fuAttachment.PostedFile.ContentType <> "video/mp4" And fuAttachment.PostedFile.ContentType <> "application/rar" And fuAttachment.PostedFile.ContentType <> "video/mpeg") Then
                    lblMsg.Visible = True
                    lblMsg.Text = "Upload JPG / BMP / JPEG / TIF / PDF / Doc / Excel / Zip format / MP4 / MPEG / MPG/ RAR / TXT / HTML / MSG files only"
                    Return False
                    Exit Try
Posted
Updated 6-Nov-17 2:02am
v2
Comments
F-ES Sitecore 6-Nov-17 7:56am    
google "c# read file magic numbers"

1 solution

Since you cannot trust the file extension, then you have to read at least some bytes of the binary file to tell its exact nature. See, for instance: List of file signatures - Wikipedia[^] and executable - How to tell if a file is an EXE or a DLL? - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900