Search My Warehouse
2010-01-08
Load Image using Ajax C# and VB.net
Create a Empty Page as
ImagePrev.aspx
And Create a new page with the below code:
<div>
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager><br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Image id="Image1" runat="server"></asp:Image>
<br />
<div id="ImagePreviewer" runat="server" style="background-position: center center;
background-image: url(Images/no-image.gif); width: 150px; background-repeat: no-repeat;
height: 150px">
</div>
<asp:FileUpload id="FileUpload1" runat="server"></asp:FileUpload>
<br />
<asp:LinkButton id="lnk_viewImg" runat="server" OnClick="lnk_viewImg_Click">View Image</asp:LinkButton>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</div>
ImagePrev.aspx.cs
protected void lnk_viewImg_Click(object sender, EventArgs e)
{
//Image1.ImageUrl = FileUpload1.
Stream stream = default(Stream);
//HttpPostedFile oFile = Request.Files(0);
HttpPostedFile oFile = Request.Files[0];
stream = oFile.InputStream;
byte[] uploadedFile = new byte[stream.Length + 1];
byte[] a = null;
stream.Read(uploadedFile, 0, Convert.ToInt32(stream.Length));
if (Session["UploadImage"] != null)
Session["UploadImage"] = uploadedFile;
else
Session.Add("UploadImage", uploadedFile);
//ImagePreviewer.Style.Item("BACKGROUND-IMAGE") = "url(ImagePrev.aspx)";
//ImagePreviewer.Style.Add("BACKGROUND-IMAGE") = "url(ImagePrev.aspx)";
ImagePreviewer.Style.Add("BACKGROUND-IMAGE", "url(ImagePrev.aspx)");
}
ImagePrev.aspx.vb
button click
' Create a stream object.
Dim stream As IO.Stream
' Get the file that was selected.
Dim oFile As HttpPostedFile = Request.Files(0)
' Place the file into the stream.
stream = oFile.InputStream
' Create a byte array for the image.
Dim uploadedFile(stream.Length) As Byte
Dim a() As Byte
' Store the image into the byte array.
stream.Read(uploadedFile, 0, stream.Length)
' Store the byte array into a session variable.
If Session.Item("UploadImage") IsNot Nothing Then
Session.Item("UploadImage") = uploadedFile
Else
Session.Add("UploadImage", uploadedFile)
End If
' Set the ImagePreviewer's url to the ImagePrev page.
'ImagePreviewer.Style.Value("BACKGROUND-IMAGE") = "url(ImagePrev.aspx)"
Me.ImagePreviewer.Style.Item("BACKGROUND-IMAGE") = "url(ImagePrev.aspx)"
Subscribe to:
Posts (Atom)