Upload & View Image in VB.Net

Upload & View Image in VB.Net



Below is the code through which you can upload an image and view it.

1) Add the below source code in HTML file-




<asp:label id="Label22" runat="server" Font-Bold="True" Font-Names="Bookman Old Style" ForeColor="Black">Choose Your File To Upload :</asp:label>
<INPUT id="MyFile" style="FONT-WEIGHT: bold; WIDTH: 357px; HEIGHT: 24px" type="file" size="40"
name="MyFile" RunAt="Server">
<INPUT id="Submit1" type="submit" value="Upload" name="Submit1" RunAt="Server">

<asp:Image ID="Image1" runat="server" Width="200" Height="200" />


2) Add NameSpaces




Imports System
Imports System.Data
Imports System.Data.SqlClient


3) Add Connection String



Dim con As New SqlConnection("initial catalog=DatabaseName;Data source=ServerName; User id=sa")



4) Now Add Below code on Upload Button in Code Behind




Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
'FileName.InnerHtml = MyFile.PostedFile.FileName
'FileContent.InnerHtml = MyFile.PostedFile.ContentType
'FileSize.InnerHtml = MyFile.PostedFile.ContentLength
'UploadDetails.Visible = True

' Let us recover only the file name from its fully qualified path at client
Image1.Visible = True
Dim strFileName As String
strFileName = MyFile.PostedFile.FileName
Dim c As String = System.IO.Path.GetFileName(strFileName)
' only the attched file name not its path

MyFile.PostedFile.SaveAs(Server.MapPath("Upload") & "\" + c)

Image1.ImageUrl = Server.MapPath("Upload") & "\" + c
'Response.Write("File Uploaded")
End Sub



Thanks
Nilesh Jadhav


Comments

Author: ABitSmart30 Nov 2009 Member Level: Gold   Points : 0

Please format your resource.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: