473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I preview a posted file?

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

Nov 14 '06 #1
2 1994
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" <Ke*********@discussions.microsoft.comwrote in message
news:49**********************************@microsof t.com...
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

Nov 15 '06 #2
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" wrote:
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" <Ke*********@discussions.microsoft.comwrote in message
news:49**********************************@microsof t.com...
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


Nov 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: HuggedRugger | last post by:
Hi, Can anyone help me with this. I have a datagrid on a web page I want to send to an excel sheet to print as a report. I send the data using command from my aspx.vb file which works...
1
by: bonnie.tangyn | last post by:
Hello All In my ASP page, I use CDONT to send email to client. But, I need to allow user preview and edit the email content before sending it out. Any advices? I have two ASP pages -...
1
by: mahfuza via DotNetMonster.com | last post by:
Hi I have an Xml file.I Load it in a Datagrid, My Xml file has child tables, and I want to print this, so I want to see its print preview, but when I want to do it, only it is shown the root table...
2
by: deepakfordotnet | last post by:
Hi, First of all let me confess that I could not get the solution to the same problem from an earlier post Printing :by Mr.Richard MSL (dated September 24th 2006) working. (Replied by Mr.Walter...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.