仅允许.Jpg和.Jpeg

时间:2013-12-04 05:20:50

标签: html asp.net vb.net

我只需要上传.Jpg和.Jpeg文件,但在上传时也允许.gif,.pdf,.txt和所有

这是我的文件上传控件,带有验证:

<td align="left" colspan="3">
 <asp:FileUpload ID="fuAttachment1" runat="server" />
 <asp:RegularExpressionValidator ID="revFile1" runat="server" ControlToValidate="fuAttachment1"
     Enabled="true" ErrorMessage="Invalid File. Please select valid file." ForeColor="Red"
    ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.JPG|.jpeg|.JPEG)$">*
  </asp:RegularExpressionValidator>
</td> </tr>

3 个答案:

答案 0 :(得分:1)

I think below expression will work try this

^.+\.(?:(?:[jJ][pP][eE][gG])|(?:[jJ][pP][gG]))$

答案 1 :(得分:0)

尝试验证表达式如下

 ValidationExpression="(.*\.([Jj][Pp][Gg])|.*\.([Jj][Pp][Ee][Gg])$)"

答案 2 :(得分:0)

您只能通过Code-Behind允许上传中的JPEG文件。

在我的示例中,我使用DetailsView和FileUpload控件(在VB代码中)

Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
     If e.AffectedRows > 0 Then
         Dim results As DataView = CType(maxEmployeeIDDataSource.Select(DataSourceSelectArguments.Empty), DataView)
         Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)
         Dim imageupload As FileUpload = CType(DetailsView1.FindControl("imageupload"), FileUpload)
         If imageupload.HasFile Then
             Dim baseDirectory As String = Server.MapPath("~/uploaded_images/")
             imageupload.SaveAs(baseDirectory & pictureIDJustAdded & ".jpg")
         End If
     End If
     End Sub



 Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
     Dim cancelInsert As Boolean = False
     Dim imageupload As FileUpload = CType(DetailsView1.FindControl("imageupload"), FileUpload)
     If Not imageupload.HasFile Then
         cancelInsert = True
     Else
         If Not imageupload.FileName.ToUpper().EndsWith(".JPG") Then
             cancelInsert = True

        End If
     End If
     If cancelInsert Then
         e.Cancel = True

    End If

我希望这可以帮到你。

相关问题