How do I preview a posted file?

G

Guest

Hi,

I would like to display a preview of a posted file.

To accomplish, this I am using storing the image bytes into cache and
setting the ImageUrl property of an image control to an aspx page named
ImagePreview.

The ImagePreview page should dynamically generates the appropriate response
but the image is not displaying properly.

I do not want to save the image on the web server.

Here is the code:

---------------------------------------------------------------------------------------------
(defalut.aspx)
---------------------------------------------------------------------------------------------
//in button_click event handler:
byte[] bytearray = null;
HttpPostedFile postedfile = FileUpload1.PostedFile;

//save the input stream as a byte array
using (System.IO.Stream stream = postedfile.InputStream)
{
int len = (int)postedfile.InputStream.Length;
bytearray = new byte[len];
stream.Read(bytearray, 0, len);
stream.Close();
}

//cache the byte array
string cachekey = Guid.NewGuid().ToString();
Cache.Add(cachekey, bytearray, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(20),
System.Web.Caching.CacheItemPriority.Default,
null);

string target = string.Format("ImagePreview.aspx?ct={0}&key={1}",

Server.UrlEncode(postedfile.ContentType),
cachekey);


imgUploadedFile.ImageUrl = target;

---------------------------------------------------------------------------------------------
(ImagePreview.aspx.cs)
---------------------------------------------------------------------------------------------
// in Page_Load event handler:
byte[] bytearray = (byte[])Cache[Request.Params["key"]];

Response.Expires = 0;
Response.Buffer = true;
Response.ClearContent();
Response.ContentType = Request.Params["ct"];

Response.BinaryWrite(bytearray);
Response.End();
=================================================

Does anyone know why the image is not displaying?

Thanks for any help,
-Keith
 
O

Onwuka Emeka

Your code works fine. try to write the url of the image to the response
stream just to make sure the imageurl is being set properly. outside that
your code does work fine, it could be something as little as the button not
being attached to the eventhandler
 
G

Guest

It doesn't work for me, I just get a broken image. The handlers are wired up
correctly, I stepped through the code to see that they bytes were being
returned.

Onwuka Emeka said:
Your code works fine. try to write the url of the image to the response
stream just to make sure the imageurl is being set properly. outside that
your code does work fine, it could be something as little as the button not
being attached to the eventhandler
Keith Harris said:
Hi,

I would like to display a preview of a posted file.

To accomplish, this I am using storing the image bytes into cache and
setting the ImageUrl property of an image control to an aspx page named
ImagePreview.

The ImagePreview page should dynamically generates the appropriate
response
but the image is not displaying properly.

I do not want to save the image on the web server.

Here is the code:

---------------------------------------------------------------------------------------------
(defalut.aspx)
---------------------------------------------------------------------------------------------
//in button_click event handler:
byte[] bytearray = null;
HttpPostedFile postedfile = FileUpload1.PostedFile;

//save the input stream as a byte array
using (System.IO.Stream stream = postedfile.InputStream)
{
int len = (int)postedfile.InputStream.Length;
bytearray = new byte[len];
stream.Read(bytearray, 0, len);
stream.Close();
}

//cache the byte array
string cachekey = Guid.NewGuid().ToString();
Cache.Add(cachekey, bytearray, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(20),
System.Web.Caching.CacheItemPriority.Default,
null);

string target = string.Format("ImagePreview.aspx?ct={0}&key={1}",

Server.UrlEncode(postedfile.ContentType),
cachekey);


imgUploadedFile.ImageUrl = target;

---------------------------------------------------------------------------------------------
(ImagePreview.aspx.cs)
---------------------------------------------------------------------------------------------
// in Page_Load event handler:
byte[] bytearray = (byte[])Cache[Request.Params["key"]];

Response.Expires = 0;
Response.Buffer = true;
Response.ClearContent();
Response.ContentType = Request.Params["ct"];

Response.BinaryWrite(bytearray);
Response.End();
=================================================

Does anyone know why the image is not displaying?

Thanks for any help,
-Keith
 
Top