Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have displayed my data in Grid view control,,and i added addition column in it for downloading particular data in that row,,,but i do not know how to download data from Grid view by click on download button which i added to grid view for each row...suppose we have one table in ms access with following fields :-
Name Photo ID
ab "~/images/ab.jpg" 1


now my question is how can i download images from Photo field via gridview column button corresspondes to particular row.

I know about downloading coding but i donot know how to implement it in Grid View
can any one tell me how to implement it in Grid View column button.



C#
string filepath = Server.MapPath("test.doc");


FileInfo file = new FileInfo(filepath);


if (file.Exists)
{

 Response.ClearContent();


 Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);


 Response.AddHeader("Content-Length", file.Length.ToString());


 Response.ContentType = ReturnExtension(file.Extension.ToLower());


 Response.TransmitFile(file.FullName);


 Response.End();
}
Posted
Updated 18-Aug-10 19:48pm
v2

1 solution

Here goes the sample implementation. I think you are kind of new to Asp.net and hence did a sample implementation that you can understand easily and develop your own implementation.

GridView in Aspx :

XML
<%@ 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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                Width="215px" OnRowCommand="GridView1_RowCommand"
                onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:ImageField DataImageUrlField="ImageUrl" HeaderText="Image">
                    </asp:ImageField>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                CommandName="Download" Text="Download"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </div>
    </form>
</body>
</html>


And, the CodeBehind :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public class Person
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string ImageUrl
        {
            get;
            set;
        }
        public Person(int Id, string Name, string ImageUrl)
        {
            this.Id = Id;
            this.Name = Name;
            this.ImageUrl = ImageUrl;
        }
    }
    //Binding some sample data to the GridView
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Populating some dummy data. In reality, you should populate these
            //from the database
            IList items = new ArrayList();

            items.Add(new Person(1, "Peter", "~/images/ab.jpg"));
            items.Add(new Person(2, "John", "~/images/cd.jpg"));
            items.Add(new Person(3, "Shubho", "~/images/ef.jpg"));

            GridView1.DataSource = items;
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            string ImageUrl = (string)e.CommandArgument;
            string physicalImagePath = Server.MapPath(ImageUrl);
            if (File.Exists(physicalImagePath))
            {
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(physicalImagePath));
                Response.ContentType = "image/jpg";

                Response.TransmitFile(physicalImagePath);
                Response.Flush();
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
            if (link != null)
            {
                link.CommandArgument = ((Person)e.Row.DataItem).ImageUrl.ToString();
            }
        }
    }
}


This example basically binds some Person data to the gridview with images (Stored inside the web application folder's /images directory). Also, it binds a command argument to the download link in GridView's RowDataBound event (See CodeBehind). When user clicks the Download link, GridView's RowCommand event gathers the Image URL from the command argument of the LinkButton, reads the image from the physical location and writes in response for download.
 
Share this answer
 
Comments
jattboyfrompunjab 19-Aug-10 5:31am    
Reason for my vote of 5
It was awesome
Toli Cuturicu 19-Aug-10 5:44am    
Then push the "Accept" button!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900