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.......

Recommended Answers

All 11 Replies

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();

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?)

Please correct it : Response.ClearHeaders()

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????

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.

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.

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........

You're welcome.

I'm glad you got it working.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.