检查asp.net中的文件扩展名

时间:2015-10-25 04:45:43

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

我正在尝试检查文件扩展名,但出于某种原因, class ChannelsTVC: UITableViewController { var channels = [String]() { didSet { tableView?.reloadData() } } // channels var myMobChatTVC = MOBChatTVC() override func viewDidLoad() { super.viewDidLoad() channels = myMobChatTVC.chatArray } // viewDidLoad } // ChannelsTVC 始终为false。

Valid

我总是得到 <asp:FileUpload ID="LogoUpload" CssClass="form-control" runat="server" /><asp:Button ID="BtnLogoUpload" runat="server" CssClass="btn btn-primary" Text="Upload" /> <asp:Label ID="lblLUploadMessage" runat="server" ForeColor="Red" ></asp:Label> Protected Sub BtnLogoUpload_Click(sender As Object, e As EventArgs) Handles BtnLogoUpload.Click If (LogoUpload.HasFile) Then Dim fileExtension As String = Path.GetExtension(LogoUpload.FileName) Dim extension = New String() {"jpg", "png", "gif"} Dim Valid As Boolean = False For Each ext As String In extension If ext.Equals(fileExtension) Then Valid = True End If Next If Valid = True Then LogoUpload.SaveAs(Server.MapPath(String.Format("{0}/{1}/{2}", "~/CW/uploads", register_username.Text, "logo") + LogoUpload.FileName)) ' Save file in directory lblLUploadMessage.Text = "File Uploaded Successfully" lblLUploadMessage.ForeColor = System.Drawing.Color.Green Else lblLUploadMessage.Text = "File extension is not valid" End If End If End Sub

1 个答案:

答案 0 :(得分:1)

方法Path.GetExtension有一个问题。如MSDN documentation中所述,此方法的返回值为:

  

指定路径的扩展名(包括句号&#34;。&#34;)或   null,或String.Empty。

这里的重要部分是 (包括句号&#34;。&#34;) 。这意味着如果您上传bmp文件,方法Path.GetExtension不会返回bmp,而是.bmp

因此,为了使其工作,您必须使用具有句点的扩展名:

Dim extension = New String() {".jpg", ".png", ".gif"}
在进行检查之前,

或从延期开始修剪期间:

Dim fileExtension As String = Path.GetExtension(LogoUpload.FileName)
fileExtension = fileExtension.TrimStart("."c)