2

I am downloading uploaded files. Below is my code

<asp:GridView ID="GridViewCV" runat="server" AutoGenerateColumns="False" DataKeyNames="attach_id" ShowHeader="false"
                        EmptyDataText="No File Found" DataSourceID="SqlDataSource1" CssClass="table-responsive">
                        <Columns>
                            <asp:BoundField DataField="attach_id" HeaderText="attach_id" ReadOnly="True" SortExpression="attach_id" />
                            <asp:BoundField DataField="attach_title" HeaderText="attach_title" SortExpression="attach_title" />
                            <asp:BoundField DataField="attach_location" HeaderText="attach_location" SortExpression="attach_location" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False" CommandArgument='<%# Eval("attach_location") %>' CommandName="Download" Text="Download" OnClick="lnkDownload_Click" CssClass="same-button" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" CommandArgument='<%# Eval("attach_id") %>' CommandName="DeleteFile" Text="Delete" OnClick="lnkDelete_Click" CssClass="same-button" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=xxxxx;Initial Catalog=xxxx;User ID=xxxx;Password=xxxx" ProviderName="System.Data.SqlClient" SelectCommand="sp_select_attachment" SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:SessionParameter DefaultValue="" Name="id" SessionField="UserId" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>



protected void lnkDownload_Click(object sender, EventArgs e)
    {
        try
        {
            LinkButton btn = (LinkButton)sender;
            string path = btn.CommandArgument;
            Response.Write(path);
            string ContentType;
            if (path != string.Empty)
            {
                string fileToDownload = "e:\\desktop\\" + path;
                string fileToRead = "e:\\desktop\\" + path;
                StreamReader ss = new StreamReader(fileToRead);
                Response.Clear();
                Response.ClearHeaders();
                Response.AddHeader("Content-Length", fileToDownload.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment;filename=" + path);
                if (path.EndsWith(".pdf"))
                {
                    ContentType = "application/pdf";
                }
                else if (path.EndsWith(".docx"))
                {
                    ContentType = "application/docx";
                }
                else
                {
                    ContentType = "application/doc";
                }
                Response.ContentType = ContentType;
                Response.WriteFile(fileToDownload);
                Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            MethodReusability.ErrorMessage(ex);//this method is saving my exception in database
        }
    }

The problem is that reload icon continuously reloads but does not download the file. I have searched for the different method but none of them worked.Here are few links from where I copied code.

Upload files, save in folder and display in ASP.Net GridView with Download and Delete option

Downloading files in asp.net using C#

File Download with save as dialog box from browser

Please tell me where I am going wrong or what I am missing. Are there any changes to make in web.config or what else I should do to download files.

AnumR
  • 85
  • 1
  • 12

0 Answers0