how to use file upload control....

Posted by Ankitsrist under ASP.NET on 12/26/2012 | Points: 10 | Views : 2254 | Status : [Member] | Replies : 22
hello
i want to use file uploader control in my master page so that user can upload photo just like socials sites and it shud display in each page until tht user session expire....can anybdy just tell how to do this
thanks in advance




Responses

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
anybody there to help.......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
my code is
 protected void Button2_Click(object sender, EventArgs e)

{
if (FileUpload1.HasFile)
{

if (FileUpload1.PostedFile.ContentType == "image/jpeg")
{
if (FileUpload1.PostedFile.ContentLength < 102400)
{
string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~//" + FileUpload1.FileName));

Label1.Text = "uploaded";
}
else
{
Label1.Text = "select file";
}
}
}
}


but problem is how to store image in database

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sinisreedhar on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
using HttpContext.Current.User.Identity.IsAuthenticated you can check the user is authenticated or not
if authenticated then give the image filepath to image control..
or
protected void Page_Load()
{
if (HttpContext.Current.Session != null)
{
if (Session.IsNewSession)
{
string cookieHeader = Request.Headers["Cookie"];
if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
{
//session expired
}
}
}
}

Ready to accept challenges at any time.....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
actually i have written this code

 protected void Button2_Click(object sender, EventArgs e)

{
if (FileUpload1.HasFile)
{

if (FileUpload1.PostedFile.ContentType == "image/jpeg")
{
if (FileUpload1.PostedFile.ContentLength < 102400)
{
db.myconnection();
int length = FileUpload1.PostedFile.ContentLength;
byte[] imagebyt = new byte[length];
HttpPostedFile img = FileUpload1.PostedFile;
img.InputStream.Read(imagebyt,0,length );
SqlCommand cmd = new SqlCommand("insert into picsupload(image)values(@image)", db.sqlcon );
cmd.Parameters.Add("@image", SqlDbType.Image).Value=imagebyt;
cmd.ExecuteNonQuery();
Label1.Text = "inserted";
}
else
{
Label1.Text = "select file";
}
}


}
}

insertion working fine....image is inserting in binary form.....but problem is when i try to show it in gridview it is not displaying...
public void fillgrid()

{
db.myconnection();
SqlCommand cmd = new SqlCommand("select image from picsupload", db .sqlcon );
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();


}

what i shud do....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
use iframe for displaying of images...

imageview.aspx

<div>
<div>
<iframe id="I1" runat="server" frameborder="0" name="I1" style="width:900px;height:475px"></iframe>
</div>
<br />
<div align="center">
<asp:Button ID="back_btn" runat="server" Text="Back To Search Results" OnClick="back_btn_Click" />

</div>
</div>


use this in pageload of imageview.aspx.cs


string url,fieldname;
fieldname= (string)Session["field_name"];
url = "~/ImageViewHandler.ashx?id=" + field_name;
I1.Attributes.Add("src", url);


use this handler to display the images
imageviewhandler

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net;


public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
SqlConnection con = con_manager.getcon();

string sql = "SELECT image FROM TABEL" +
"WHERE field_name=@variable";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@variable", SqlDbType.VarChar).Value = context.Request.QueryString["id"];

SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()) //yup we found our image
{
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite((byte[])dr["image"]);

}
con.Close();
}

public bool IsReusable {
get {
return false;
}
}

}

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
thanks @Chaithragm.....i'll try and let u know my result

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
how to get this iframe......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
how to get this iframe......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
use the tags... the code i have posted above in aspx page

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
many errors are coming like name and align in iframe are outadated.....and wat to write in button event handler

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
urs is windows application or web application ?
the buttton click is for just to go back that is it .you can redirect to the page you want

protected void back_btn_Click(object sender, EventArgs e)
{
Response.Redirect("ImageSearchBack.aspx");
}

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
web application......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
my code for file upload control....to insert image into the database.....and it working fine image is inserting....but problem with the displaying image in iframe.....i will show u my httphandler
dbClass db = new dbClass();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string url, fieldname;
fieldname = (string)Session["Loginname"];
url = "~/ImageViewHandler.ashx?id=" + fieldname;
frame1.Attributes.Add("src", url);


}

}
protected void Button2_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{

if (FileUpload1.PostedFile.ContentType == "image/jpeg")
{
if (FileUpload1.PostedFile.ContentLength < 102400)
{
db.myconnection();
int length = FileUpload1.PostedFile.ContentLength;
byte[] imagebyt = new byte[length];
HttpPostedFile img = FileUpload1.PostedFile;
img.InputStream.Read(imagebyt,0,length );
SqlCommand cmd = new SqlCommand("insert into picsupload(image)values(@image)", db.sqlcon );
cmd.Parameters.Add("@image", SqlDbType.Image).Value=imagebyt;
cmd.ExecuteNonQuery();
Label1.Text = "inserted";
}
else
{
Label1.Text = "select file";
}
}


}


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
my handler.ashx as follows.......might be it is creating problem

using System.Net;


public class Handler : IHttpHandler
{
dbClass db = new dbClass();
public void ProcessRequest (HttpContext context)
{
db.myconnection();
string imageid = context.Request.QueryString["id"];


SqlCommand command = new SqlCommand("select Image from picsupload where Imageid=" + imageid, db.sqlcon );
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);

context.Response.End();

}




Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
in the handler the fieldname must be the unique coloumn from the tabel....
for eg imageid or row_id

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
Hope these files may help you...
first we are uploading the image to the database
then displaying images in iframe
 Download source file

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
yes sir i have given it as identity which increments by 1......but when page gets load it show the error in frame that resource can not be found

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
ok........

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
but sir for web application we shud use the same frame or something else bcoz it is showing scroll bars.....which does not generaly visible in social sites

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
but sir for web application we shud use the same frame or something else bcoz it is showing scroll bars.....which does not generaly visible in social sites

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
thanks sir for helping me

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/27/2012 [Member] Starter | Points: 25

Up
0
Down
thanks sir for helping me

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response