0

I have a problem with download links regarding a gridview.

This is my gridview:

<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" AutoGenerateColumns="False" EmptyDataText = "No files uploaded" style="margin-left: 4px; margin-top: 231px" Width="1021px" Height="323px" AllowSorting="True">
            <columns>
                <asp:boundfield datafield="ID" headerText  ="Rec Id"/>
                <asp:boundfield datafield="Center" headertext  ="Work Center"/>
                <asp:boundfield datafield="Model" headertext="Model"/>
                <asp:boundfield datafield="No" headertext="Order No"/>
                <asp:boundfield datafield="Name" headertext="Part Name"/>
                <asp:boundfield datafield="Description" headertext="Occurance Description"/>
                <asp:boundfield datafield="Rate" headertext="Def Rate"/>
                <%-- <asp:boundfield datafield="Files" headertext="Files"/> --%> 
                <asp:TemplateField HeaderText="Files">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Files" + " " + " ") %>' CommandName="Download" Text='<%# Eval("Files" + " ") %>' OnClick = "lnkDownload_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </columns>
</asp:GridView>

This is my .cs code behind:

 protected void lnkDownload_Click(object sender, EventArgs e)
    {

        //string filePath = (sender as LinkButton).CommandArgument;
        string filePath = (sender as LinkButton).CommandArgument;
        //string[] directories =filePath.Split(Path.DirectorySeparatorChar);
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
        Response.WriteFile(filePath);
        Response.End();
    }

When I try to download multiple files at a time I get an error.I want to create download link for multiple files having having the same ID from the database.The files are stored in a folder! I want to split the links regarding the files! Thanks for your help!

Mihai
  • 26,325
  • 7
  • 66
  • 81
  • 3
    For each request, you can only return a single response. If you want to return multiple files, then you will have to zip them into a single file before sending them back to the client. – krillgar Sep 29 '15 at 11:51
  • @krillgar is correct.check the link http://stackoverflow.com/questions/834527/how-do-i-generate-and-send-a-zip-file-to-a-user-in-c-sharp-asp-net – shreesha Sep 29 '15 at 11:54
  • Thanks Krillgar!Than means that I have to use zip file method?Right? – Cristian83 Sep 29 '15 at 12:46
  • Thanks Krillgar!Than means I have to use zip file method?Right?Thanks for your answer! – Cristian83 Sep 29 '15 at 12:48
  • If I try to download one file works,but my problem appears when I try to download multiple files from the same path! – Cristian83 Sep 29 '15 at 12:50
  • Dude the path doesnt matter. You just cant pass multiple single files as an attachment to the header. It needs to be a single one. So if you've got multiple files, create an archive (zip/rar). You then have to pass the archive as the attachment. Just what @krillgar said. – C4d Sep 29 '15 at 14:01
  • Yes,very clear answer!Thanks!Krillgar and C4ud3x! – Cristian83 Sep 29 '15 at 14:26

0 Answers0