I have a simple webform where a user can upload images by using an input
element of type "file". In the a button's click event in the codebehind is
this code which saves the file to the server. Everything works OK. My
concern is how can I be sure the user is really uploading an image and not a
file with some malicious code in it. Also, can someone tell me what my
security concerns are here?
Here's the html:
<form id="Form1" encType="multip art/form-data" runat="server">
Select File to Upload: <input id="uploadedFil e" type="file"
name="uploadedF ile" runat="server" style="WIDTH: 592px; HEIGHT: 24px"
size="79">
<p><input id="upload" type="button" value="Upload" name="upload"
runat="server">
</p>
<asp:label id="message" runat="server"> </asp:label>
</form>
and here's the code behind:
Private Sub upload_ServerCl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles upload.ServerCl ick
If Not (uploadedFile.P ostedFile Is Nothing) Then
Try
Dim savePath As String = Server.MapPath( ".") & "\images\te st\"
Dim postedFile = uploadedFile.Po stedFile
Dim filename As String = Path.GetFileNam e(postedFile.Fi leName)
Dim contentType As String = postedFile.Cont entType
Dim contentLength As Integer = postedFile.Cont entLength
postedFile.Save As(savePath & filename)
message.Text = postedFile.File name & " uploaded" & _
"<br>conten t type: " & contentType & _
"<br>conten t length: " & contentLength.T oString()
Catch exc As Exception
message.Text = "Failed uploading file: " &
exc.InnerExcept ion.ToString
End Try
End If
End Sub
--
moondaddy@nospa m.com
element of type "file". In the a button's click event in the codebehind is
this code which saves the file to the server. Everything works OK. My
concern is how can I be sure the user is really uploading an image and not a
file with some malicious code in it. Also, can someone tell me what my
security concerns are here?
Here's the html:
<form id="Form1" encType="multip art/form-data" runat="server">
Select File to Upload: <input id="uploadedFil e" type="file"
name="uploadedF ile" runat="server" style="WIDTH: 592px; HEIGHT: 24px"
size="79">
<p><input id="upload" type="button" value="Upload" name="upload"
runat="server">
</p>
<asp:label id="message" runat="server"> </asp:label>
</form>
and here's the code behind:
Private Sub upload_ServerCl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles upload.ServerCl ick
If Not (uploadedFile.P ostedFile Is Nothing) Then
Try
Dim savePath As String = Server.MapPath( ".") & "\images\te st\"
Dim postedFile = uploadedFile.Po stedFile
Dim filename As String = Path.GetFileNam e(postedFile.Fi leName)
Dim contentType As String = postedFile.Cont entType
Dim contentLength As Integer = postedFile.Cont entLength
postedFile.Save As(savePath & filename)
message.Text = postedFile.File name & " uploaded" & _
"<br>conten t type: " & contentType & _
"<br>conten t length: " & contentLength.T oString()
Catch exc As Exception
message.Text = "Failed uploading file: " &
exc.InnerExcept ion.ToString
End Try
End If
End Sub
--
moondaddy@nospa m.com
Comment