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
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