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."
Else
ImageName = id & ".jpg"
strFilePath = Server.MapPath(imgfolder) & ImageName
ImgUpload.PostedFile.SaveAs(strFilePath)
'Dim success As Integer = ResourcesHelper.UpdateResources(ResourcesTitle, ResourcesDimension, ImageName, ZoomImageName)
Dim success As Integer = True
If success Then
MultiView1.SetActiveView(View2)
End If
End If
Else
ImgError.ControlStyle.ForeColor = Drawing.Color.Red
ImgError.Text = "The image uploaded must be jpeg."
End If
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.
Smadhu
Member
510 Points
985 Posts
how to check fileupload has file or not and also i want to change only name of image
Feb 13, 2012 04:25 AM|LINK
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." Else ImageName = id & ".jpg" strFilePath = Server.MapPath(imgfolder) & ImageName ImgUpload.PostedFile.SaveAs(strFilePath) 'Dim success As Integer = ResourcesHelper.UpdateResources(ResourcesTitle, ResourcesDimension, ImageName, ZoomImageName) Dim success As Integer = True If success Then MultiView1.SetActiveView(View2) End If End If Else ImgError.ControlStyle.ForeColor = Drawing.Color.Red ImgError.Text = "The image uploaded must be jpeg." End IfikeAndy
Member
40 Points
29 Posts
Re: how to check fileupload has file or not and also i want to change only name of image
Feb 23, 2012 02:12 AM|LINK
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
Next
Hope this is what you had in mind.