how to check fileupload has file or not and also i want to change only name of image RRS feed

  • Question

  • User-1458744719 posted
    below code i am using to check whether fileupload has image or not but its only showing 1 message pla upload image 
    and also i only want to change the image name not the extension how can i do that
    plz do help with the code
    <asp:Label ID="lblResourcesImg" class="lbl" runat="server" Text="Upload Image"></asp:Label>
    <asp:FileUpload ID="ImgUpload" runat="server" CssClass="Upload" EnableViewState="true" /> 
    <asp:Label ID="ImgError" runat="server" Text="The Image must be with 125X125px in dimension."></asp:Label>
    vb code 
      If ImgUpload.HasFile Then
                Dim UploadedImageType As String = ImgUpload.PostedFile.ContentType.ToString().ToLower()
                Dim UploadedImageFileName As String = ImgUpload.PostedFile.FileName
                Dim UploadedImage As System.Drawing.Image = System.Drawing.Image.FromStream(ImgUpload.PostedFile.InputStream)
                Dim UploadedImageWidth As Single = UploadedImage.PhysicalDimension.Width
                Dim UploadedImageHeight As Single = UploadedImage.PhysicalDimension.Height
                If UploadedImageType = "image/jpeg" Or UploadedImageType = "image/png" Then
                    If UploadedImageWidth <> 125 Or UploadedImageHeight <> 125 Then
                        ImgError.ControlStyle.ForeColor = Drawing.Color.Red
                        ImgError.Text = "The image uploaded must be 125px X 125px in dimension."
                        ImageName = id & ".jpg"
                        strFilePath = Server.MapPath(imgfolder) & ImageName
                        'Dim success As Integer = ResourcesHelper.UpdateResources(ResourcesTitle, ResourcesDimension, ImageName, ZoomImageName)
                        Dim success As Integer = True
                        If success Then
                        End If
                    End If
                    ImgError.ControlStyle.ForeColor = Drawing.Color.Red
                    ImgError.Text = "The image uploaded must be jpeg."
                End If
    Monday, February 13, 2012 12:25 AM


  • User482687280 posted

    I'm not sure about changing the file name and not the extention but this should work to see if the file is the correct type. This will work if you have multiple fileuploads but it'll work just fine for only one as well.


    For Each fileKey As String In HttpContext.Current.Request.Files.Keys

      Dim file As HttpPostedFile = HttpContext.Current.Request.Files(fileKey)

      If (file.ContentLength > 0) Then 'Skip unused file controls.

        If file.ContentType = "image/jpeg" Then

          'do whatever here when it is a jpeg

        End If

     End If


    Hope this is what you had in mind.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2012 10:12 PM