How to upload file with a linkbutton click

Posted by Jerome under ASP.NET on 9/10/2012 | Points: 10 | Views : 5018 | Status : [Member] | Replies : 2
I have an image box and a linkbutton.What I want is when I click the linkbutton I want to browse files from my directory and select a picture.After selecting,the picture will be shown in the image box.Please give me code for this.




Responses

Posted by: Tamilarasu on: 9/10/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

You Have to Create a Generic Handles (Http Handler)*.ashx File To bind binary images to a control

using System;
using System.Web;

public class ImageHandler : IHttpHandler, IReadOnlySessionState
{

public void ProcessRequest(HttpContext context)
{
context.Response.BinaryWrite(yourImageByte);// You must replace "yourImageByte" with image reader logic from database.
}

public bool IsReusable {
get { return false; }
}

}


And After This You can Bind the Image To the handler
Image1.ImageUrl ="ImageHanderl.ashx"


Use This Code To Upload The Image


protected void UploadThisFile(FileUpload upload)
{
if (upload.HasFile)
{
string theFileName = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
if (File.Exists(theFileName))
{
File.Delete(theFileName);
}
upload.SaveAs(theFileName);
}
}


protected void buttonUpload_Click(object sender, System.EventArgs e)
{
UploadThisFile(FileUpload1);

}

protected void Button1_Click(object sender, EventArgs e)
{

string ServerMapPath = Server.MapPath("~//images//"+FileUpload1.FileName);

FileUpload1.PostedFile.SaveAs(ServerMapPath);
}

.Aspx Code For Button Event

<asp:fileupload id="FileUpload1" runat="server" /><br />
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Upload Image:" />


Thanks,
Tamilarasu

Work Hard To Work Good !... .. ...

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

Posted by: Tamilarasu on: 9/10/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Just Look At This Code .
Copy and and paste it , and see how it works . You May Get An Idea With . This Example Works Fine

This is Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

<style type="text/css">

.style1

{

width: 100%;

}

</style>

</head>

<body>

<form id="form1" runat="server">

<table class="style1">

<tr>

<td>

Enter Folder name

</td>

<td>

<asp:TextBox ID="txt_foldername" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Button ID="btn_submit" runat="server" Text="Create Folder"

onclick="btn_submit_Click" />

</td>

</tr>

<tr>

<td>

Select Image</td>

<td>


<asp:FileUpload ID="FileUpload1" runat="server" />


</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Button ID="btn_fileupload" runat="server" Text="Upload Image"

onclick="btn_fileupload_Click" />

&nbsp;&nbsp;&nbsp;

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="105px"

onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="109px">

</asp:ListBox>

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Image ID="Image1" runat="server" Height="156px" Width="202px" />

</td>

</tr>

<tr>

<td>

&nbsp;</td>

<td>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"

Text="Delete Selected Image" />

</td>

</tr>

</table>

<div>

</div>

</form>

</body>

</html>

This is the .cs code


using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.IO;

public partial class _Default : System.Web.UI.Page
{

String fn;

Int32 cnt;

Int32 i;

String pth;

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btn_submit_Click(object sender, EventArgs e)

{

// creating folder dynamically on server

if (Directory.Exists(Server.MapPath(txt_foldername.Text)))

return; // if allready exists then it will not create it

else

// if not created then it will create it.

Directory.CreateDirectory(Server.MapPath(txt_foldername.Text));

}

protected void btn_fileupload_Click(object sender, EventArgs e)

{

String save; // storing the path of the image filename

// if image file size is greater then below condition will execute it.

if (FileUpload1.PostedFile.ContentLength > 0)

{

fn = Path.GetFileName(FileUpload1.FileName); // here fn will store the image filename

save = Server.MapPath(txt_foldername.Text) + "/" + fn; // making the path with created dynamically folder name

FileUpload1.SaveAs(save); // will save the image inside the dynamically created folder name

Int32 cnt;

Int32 i;

cnt = ListBox1.Items.Count; // will count the total collections in listbox



if (cnt == 0) // if no item is added in the listbox then it will save it and exit (return) from the code.

{ ListBox1.Items.Add(fn); return; }

//if listbox item is not empty the following code will execute.

for (i = 0; i <= cnt - 1; i++)

{

// check the filename if it is already exists in the listbox then it will not add it in.

if (ListBox1.Items[i].Text == fn)

{

Response.Write("file name already exists");

return;

}

}

// if not exists in the listbox then it will add it in listbox

ListBox1.Items.Add(fn);

}

}

protected void Button1_Click(object sender, EventArgs e)

{

cnt = ListBox1.Items.Count; // count the items in listbox

for (i = 0; i <= cnt - 1; i++)

{

if (ListBox1.Items[i].Selected==true)

{ //will delete the image from the dynamically created folder.

pth=(Server.MapPath(txt_foldername.Text))+"/"+ListBox1.Text;

File.Delete(pth); //deleting the file from server

ListBox1.Items.Remove(ListBox1.Text); //removing the selected item in listbox.

return;

}

}

}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)

{

cnt = ListBox1.Items.Count; // count the items in listbox

for (i = 0; i <= cnt - 1; i++)

{

if (ListBox1.Items[i].Selected == true)

{

// Will display the selected image

Image1.ImageUrl = txt_foldername.Text + "/" + ListBox1.Items[i].Text;

}}}}


Thanks,
Tamilarasu


Work Hard To Work Good !... .. ...

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

Login to post response