我有一个子上传文件。如何限制可以上传的文件类型?我不希望用户能够上传.exe
,.dll
,.ini
个文件。现在可以上传任何文件。
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs("C:\Uploads\" & _
FileUpload1.FileName)
Label1.Text = "File name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & "<br>" & _
"Content type: " & _
FileUpload1.PostedFile.ContentType & "<br>" & _
"Location Saved: C:\Uploads\" & _
FileUpload1.FileName
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString()
End Try
Else
Label1.Text = "You have not specified a file."
End If
End Sub
答案 0 :(得分:0)
If FileUpload1.HasFile Then
Try
if FileUpload1.FileName.ToLower().Contains(".exe") or FileUpload1.FileName.ToLower().Contains(".dll") or FileUpload1.FileName.ToLower().Contains(".ini")
' show your alert here stating this type of files are not allowed
return
End if
FileUpload1.SaveAs("C:\Uploads\" & _
FileUpload1.FileName)
Label1.Text = "File name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & "<br>" & _
"Content type: " & _
FileUpload1.PostedFile.ContentType & "<br>" & _
"Location Saved: C:\Uploads\" & _
FileUpload1.FileName
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString()
End Try
Else
Label1.Text = "You have not specified a file."
End If
But, I would suggest you client side check will be better too.