文件下载代码没有给出错误,但仍无法下载文件

时间:2018-01-23 04:48:17

标签: c# asp.net gridview sender asplinkbutton

我正在下载上传的文件。以下是我的代码

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

问题是重新加载图标会不断重新加载,但不会下载文件。 我搜索了不同的方法,但没有一个工作。这里是我复制代码的链接。

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

请告诉我哪里出错了或我错过了什么。是否在web.config中进行了任何更改,或者我还应该做些什么来下载文件。

0 个答案:

没有答案