954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Download Files From Gridview

Hello to all,

I've designed a website to provide Software and all the data is coming into the gridview.The entries in database are......

SoftName : Name of the software

SoftType : Type of the software (like windows,mac etc.)

SoftUrl : Path of the software file in the website
(It is a folder "Soft")

Now I want to add a download link into the gridview that'll make the user to download directly my software from the Gridview.

Thanks in advance.......

KM499
Junior Poster
100 posts since May 2010
Reputation Points: 8
Solved Threads: 3
 

See,

string filename="test.zip";
string path=MapPath("~/soft/" + filename);

byte []bts=System.IO.File.ReadAllBytes(path);

Response.Clear();
Response.ClearHeader();

Response.AddHeader("Content-Type","Application/octet-stream")
Response.AddHeader("Content-Length",bts.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=" + filename);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Error coming as..........

'System.Web.HttpResponse' does not contain a definition for 'ClearHeader' and no extension method 'ClearHeader' accepting a first argument of type 'System.Web.HttpResponse' could be found (are you missing a using directive or an assembly reference?)
KM499
Junior Poster
100 posts since May 2010
Reputation Points: 8
Solved Threads: 3
 

Please correct it : Response.ClearHeaders()

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Its for a single file i.e., test.zip.......But I'm getting a list of software stuff from the database into the gridview........How can I code for every file????

KM499
Junior Poster
100 posts since May 2010
Reputation Points: 8
Solved Threads: 3
 

You must have to read those links which I have posted in your previous thread. You need to handle the event of GridView control.

Markup

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowcommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" 
                            CommandArgument='<%# Eval("url") %>' CommandName="cmd">Download</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


Code-behind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmd")
        {
            string filename=e.CommandArgument.ToString();
            string path=MapPath("~/soft/" + filename);
            byte []bts=System.IO.File.ReadAllBytes(path);

            Response.Clear();
            Response.ClearHeaders();

            Response.AddHeader("Content-Type", "Application/octet-stream");
            Response.AddHeader("Content-Length",bts.Length.ToString());
            Response.AddHeader("Content-Disposition","attachment; filename=" + filename);
            Response.BinaryWrite(bts);
            Response.Flush();
            Response.End();
        }
    }


PS: Try to mark thread as solved if you have found an answer to your question.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

You must have to read those links which I have posted in your previous thread. You need to handle the event of GridView control.

Markup

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowcommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" 
                            CommandArgument='<%# Eval("url") %>' CommandName="cmd">Download</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


Code-behind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmd")
        {
            string filename=e.CommandArgument.ToString();
            string path=MapPath("~/soft/" + filename);
            byte []bts=System.IO.File.ReadAllBytes(path);

            Response.Clear();
            Response.ClearHeaders();

            Response.AddHeader("Content-Type", "Application/octet-stream");
            Response.AddHeader("Content-Length",bts.Length.ToString());
            Response.AddHeader("Content-Disposition","attachment; filename=" + filename);
            Response.BinaryWrite(bts);
            Response.Flush();
            Response.End();
        }
    }


PS: Try to mark thread as solved if you have found an answer to your question.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

I must say that you're a total genius for me bro...This was the only problem that I was facing for many days and because of you I've found my solution and the end of my project too....May u get whatever you need........Thanks a lot man........

KM499
Junior Poster
100 posts since May 2010
Reputation Points: 8
Solved Threads: 3
 

You're welcome.

I'm glad you got it working.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

hi,

i'm trying to use your code,
but i don't know what should i write instead of the: <%# Eval("url") %>

can you pls help?

thanks.

OfirAs
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

adatapost. you're a super hero. U solved my problem. this is what exactly i wanted. May you long live with whatever u wanted in your life

Mahendrabalan
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Hello,

This "url" refers to the path of the folder contained in the project like in my case it was a folder named "Soft" and hence I wrote <%# Eval("~/Soft/") %>
Hope that helps.

Thanks in advance.

KM499
Junior Poster
100 posts since May 2010
Reputation Points: 8
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You