using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace InsertImage
{
public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//Checking whether the imagebytes variable have anything else not doin anything
if ((context.Session["ImageBytes"]) != null)
{
byte[] image = (byte[])(context.Session["ImageBytes"]);
context.Response.ContentType = "image/JPEG";
context.Response.BinaryWrite(image);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Please note that FileUpload control doesn't have viewstate capability i.e. it will not retain the content in between postbacks as other asp.net controls like textbox, checkbox etc do. So when you click btnPreview, the page would postback again and in that
instane FileUpload control will be empty. So no FileBytes.
In order to save filebytes in session, do this on the event of the button that is uploading the file and not previewing it.
P.S. Session memory is a valueable resource, so I'd not recommend storing filebytes in it.. because it is limited and file sizes can be huge..
You could not do like this because whenver you want to upload the file at server end you need to postback the page for server side code.
if you want use handler then you can use jquery file uploder because it create one form and when you upload the file, it will call that page code (handler or serverside) and post that file to server.
you could it in javascript with your code.
MARK AS ANSWER....IF THIS WORKS....
Mark Answered if it helps - Good luck!
Cheers!
- Kaushik Patel
deepthoughts, Sorry, you canexplainwhat it means to
do this on the event of the button that is uploading the file and not previewing it.?I justneed a
preview
ReSonance
Member
10 Points
84 Posts
FileUpload Image
Dec 17, 2012 05:23 AM|LINK
Here's a question - how do I do that after previewing images before downloading does not erase the way in Fileupload?
(In order to not have to re-select the images) for preview using this method:
protected void btnPreview_Click(object sender, EventArgs e) { Session["ImageBytes"] = PhotoUpload.FileBytes; ImagePreview.ImageUrl = "~/ImageHandler.ashx"; }ImageHandler.ashx:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace InsertImage { public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { public void ProcessRequest(HttpContext context) { //Checking whether the imagebytes variable have anything else not doin anything if ((context.Session["ImageBytes"]) != null) { byte[] image = (byte[])(context.Session["ImageBytes"]); context.Response.ContentType = "image/JPEG"; context.Response.BinaryWrite(image); } } public bool IsReusable { get { return false; } } } }And I deduce the conventional image:
sameer_khanj...
Contributor
7060 Points
1378 Posts
Re: FileUpload Image
Dec 17, 2012 06:08 AM|LINK
I think after postback maintainng values in uploader is not possible , you have to findout alternative ways like :
1) maintain binary in cahce, session, viewstate as per your convinience.
2) after preview close show selected file path in lable and provide upload button and cancel
3) on cancel click show the fileupload control and remove lable.
4) and on click of upload button, same image on server or in database.
sameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
deepthoughts
Contributor
7288 Points
1051 Posts
Re: FileUpload Image
Dec 17, 2012 09:00 AM|LINK
Please note that FileUpload control doesn't have viewstate capability i.e. it will not retain the content in between postbacks as other asp.net controls like textbox, checkbox etc do. So when you click btnPreview, the page would postback again and in that instane FileUpload control will be empty. So no FileBytes.
In order to save filebytes in session, do this on the event of the button that is uploading the file and not previewing it.
P.S. Session memory is a valueable resource, so I'd not recommend storing filebytes in it.. because it is limited and file sizes can be huge..
Thanks.
ReSonance
Member
10 Points
84 Posts
Re: FileUpload Image
Dec 17, 2012 10:45 AM|LINK
deepthoughts, I do not understand what is the preview button to impose load. It can be possible as it is with most Onchange Fileupload ? For example:
<script> function callme(oFile) { Session["ImageBytes"] = imgUpload.FileBytes; ImagePreview.ImageUrl = "~/ImageHandler.ashx"; </script> <asp:FileUpload ID="imgUpload" runat="server" onchange="callme(this)" />Only in this way does not work, if possible please help me write the correct onchange
deepthoughts
Contributor
7288 Points
1051 Posts
Re: FileUpload Image
Dec 17, 2012 11:19 AM|LINK
Are you serious? Can server side code be called from javascript like this? Unless page posts back, you can't access FileBytes on server.
Thanks.
kaushik_tatv...
Contributor
2808 Points
500 Posts
Re: FileUpload Image
Dec 17, 2012 11:25 AM|LINK
You could not do like this because whenver you want to upload the file at server end you need to postback the page for server side code.
if you want use handler then you can use jquery file uploder because it create one form and when you upload the file, it will call that page code (handler or serverside) and post that file to server.
you could it in javascript with your code.
MARK AS ANSWER....IF THIS WORKS....
Cheers!
- Kaushik Patel
ReSonance
Member
10 Points
84 Posts
Re: FileUpload Image
Dec 25, 2012 03:17 AM|LINK
deepthoughts, Sorry, you can explain what it means to do this on the event of the button that is uploading the file and not previewing it.? I just need a preview