Binding the image Control with byte
File upload control on client side show image using byte array
//File upload control on change to post the method
fileupload control and image control should be in update panel
<asp:FileUpload ID="FileUpload1" runat="server" onchange="this.form.submit();"
image control on page
// on page load
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.
{
UploadImage();
}
// upload image method
void UploadImage()
{
//Session["File"] = fluPicture.FileBytes;
//Session["Name"] = fluPicture.FileName;
// string scanletterFileName = "";
string letterUploadFileName = System.IO.Path.GetFileName(
try
{
string[] fileType = FileUpload1 .PostedFile.FileName.Split('.'
int letterI = fileType.Length;
byte[] letterUpload = new byte[FileUpload1 .PostedFile.ContentLength];
if (fileType[letterI - 1].ToUpper().Trim() == "PDF" || fileType[letterI - 1].ToUpper().Trim() == "DOC" || fileType[letterI - 1].ToUpper().Trim() == "JPG" || fileType[letterI - 1].ToUpper().Trim() == "GIF" || fileType[letterI - 1].ToUpper().Trim() == "JPEG" || fileType[letterI - 1].ToUpper().Trim() == "XLS")
{
letterUpload = new byte[FileUpload1 .PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1 .PostedFile;
Image.InputStream.Read(
lblmsg.Text = letterUploadFileName;
Session["image"] = letterUpload;
Session["filename"] = letterUploadFileName;
//lblScanLetter.Text = letterUploadFileName;
//Response.Redirect("
//img.ImageUrl="showimage.
img.ImageUrl = "ShowImage.aspx";
}
}
catch (Exception ex)
{
lblmsg.Text = "An Error Occured. Please Try Again!" + ex.Message;
}
finally
{
}
}
//showimage.aspx
//on page load
byte[] myimage = (byte[])Session["image"];
string filename = Session["filename"].ToString()
FillbinaryData(myimage , filename);
//FillbinaryDate metho
protected void FillbinaryData(byte[] image,string Filename)
{
try
{
// Dcon.OpenConnection();
// drHeads = t1.ExecuteDReader(searchQuery, Dcon.GetConnection);
// drHeads = t1.ExecuteDReader(searchQuery, Dcon.GetConnection);
// while (drHeads.Read())
//{
// Response.ClearContent();
// Response.ClearHeaders();
if (image != null )
{
string[] contentType = Filename.ToString().Split('.')
int i = contentType.Length;
switch (contentType[i - 1].ToUpper().Trim())
{
case "PDF":
Response.ContentType = "application/pdf";
Response.AddHeader("content-
break;
case "DOC":
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("content-
break;
case "DOCX":
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("content-
break;
case "XLS":
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-
break;
case "XLSX":
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-
break;
default:
break;
}
byte[] buffer = null;
buffer = (byte[])image ;
Response.Charset = "";
Response.Cache.
Response.BinaryWrite(buffer);
Response.End();
}
else
Response.Write("No Data with Specified Request");
//}
}
catch (Exception ex)
{
lblmsg.Text = ex.ToString();
}
finally
{
//drHeads.Close();
//Dcon.CloseConnection();
}
}
use below code to the Image Preview ( Temporary )
ReplyDeleteJavascript :
Step 1 :
function uploadImage(ctr)
{
var ar= ctr.value.split('.');
var conType=ar[ar.length-1];
if(conType.toLowerCase() == 'jpg' && ctr.files.item(0).size < 1024000 )
{
ctr.form.submit();
}
else
{
alert('Image Should be jpg format and Size less than 1 MB');
}
}
Step 2 :
Step 3 : onpage load
if (FileUpload1.HasFile)
{
UploadedFile objUploadedFile = new UploadedFile();
objUploadedFile.FileBytes = FileUpload1.FileBytes;
objUploadedFile.FileName = FileUpload1.FileName;
Session["UploadedFile"] = objUploadedFile;
Image1.ImageUrl = "ImagePreview.aspx";
}
Step 4 : in ImagePreview.aspx form on load event
UploadedFile objUploadedFile = (UploadedFile)Session["UploadedFile"];
Response.ContentType = ".jpg";
Response.BinaryWrite(objUploadedFile.FileBytes);
Response.End();
Step 5 :UploadedFile.cs (Class File)
public byte[] FileBytes { get; set; }
public string FileName { get; set; }
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete