【问题标题】:ASP.NET VB , how to restrict filetype to upload?ASP.NET VB,如何限制文件类型上传?
【发布时间】:2016-04-11 09:10:57
【问题描述】:

我有一个子上传文件。如何限制可以上传的文件类型?我不希望用户能够上传.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

【问题讨论】:

标签: asp.net vb.net file-upload


【解决方案1】:
    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.

【讨论】:

  • 欢迎,不要忘记投票并接受答案。 :)
  • 如果有人将文件扩展名从 exe 更改为 txt,它将上传。这里的解决方案是什么。
猜你喜欢
  • 2011-11-11
  • 1970-01-01
  • 2012-01-19
  • 2010-12-16
  • 1970-01-01
  • 2014-03-21
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多