问题描述:

Hi i'm trying to display a preview of an uploaded img in asp.net using the file upload control, i have a preview button where when the user clicks should save the file from the fileupload control into a temp folder on my server and change the imageurl of the imagecontrol to the path of the saved image in the temp folder and display the image on the page after a postback. Here is what i have done:

protected void btnPreview_1_Click(object sender, EventArgs e)

{

if (!imgUpload_1.HasFile)

{

return;

}

string strFileName = Path.GetFileNameWithoutExtension(imgUpload_1.FileName.ToString());

string ext = Path.GetExtension(imgUpload_1.FileName.ToString());

string loc = "temp/";

string strImgFolder = Server.MapPath(loc);

System.Drawing.Image newImage;

FileUpload img = (FileUpload)imgUpload_1;

Byte[] imgByte = null;

try

{

if (img.HasFile && img.PostedFile != null)

{

HttpPostedFile File = imgUpload_1.PostedFile;

imgByte = new Byte[File.ContentLength];

File.InputStream.Read(imgByte, 0, File.ContentLength);

}

}

catch (Exception ex)

{

}

if (imgByte != null)

{

using (MemoryStream stream = new MemoryStream(imgByte, 0, imgByte.Length))

{

newImage = System.Drawing.Image.FromStream(stream);

newImage.Save(strImgFolder + strFileName + ext);

imgPreview.ImageUrl = strImgFolder + strFileName + ext;

imgPreview.AlternateText = strFileName;

}

}

}

I've tried using a handler for this using code i found online and after hours of trying i still couldn't get it to work, i also tried attaching a random number to the end of the imageurl(like so imageurl + "?" + randomnumber to force the page to refresh(That didn't work either) and i also tried:

Response.AddHeader("Cache-Control", "no-cache");

in my page load even and that didn't work either.

I've been at it for hours could someone plz help me out?

相关阅读:
Top