用WebForm名称中的.aspx扩展名下载文件



在我的 WebForm(viewRequest.aspx)上,我放置了一个gridview,用户可以从网格下载文件。

文件在网格中填充,delete和>下载 button.ut.但是当用户从 Google Chrome 文件名称始终ViewRequest.aspx 是页面名称。然后,用户必须使用机制进行打开,才能使用合适的格式打开所选文件,例如PDF文件等。

在Internet Explorer中没有出现此问题。文件名与网格中的文件名相同,扩展名也可以。

我不知道会有什么问题。

任何人都可以帮助我。

这是我下载文件的代码

protected void Grid_Attachments_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "download")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string url = Grid_Attachments.Rows[index].Cells[3].Text;
        System.IO.FileInfo file = new System.IO.FileInfo(url);
        if (file.Exists)
        {
            Response.Clear();
            Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name);
            Response.AppendHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.TransmitFile(file.FullName);
            Response.End();
        }
    }
}

这是我的ASPX页面代码:

<asp:GridView ID="Grid_Attachments" runat="server" Width="100%" AutoGenerateColumns="False" EnableModelValidation="True" OnRowDataBound="Grid_Attachments_RowDataBound" OnRowDeleting="Grid_Attachments_RowDeleting" OnRowCommand="Grid_Attachments_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="Files Added">
                                    <ItemTemplate>
                                        <asp:Label ID="LBL_Attachment" runat="server" Text='<%# Bind("FILE_NAME") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
                                    <ItemStyle CssClass="UserTableRow" />
                                </asp:TemplateField>
                                  <asp:BoundField HeaderText="Added By" DataField="empname">
                                    <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="25%" />
                                    <ItemStyle CssClass="UserTableRow" />
                                </asp:BoundField>
                                  <asp:BoundField HeaderText="Attachment Date" DataField="ATTACHMENT_DATE">
                                    <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="20%" />
                                    <ItemStyle CssClass="UserTableRow" />
                                </asp:BoundField>
                                <asp:BoundField HeaderText="File Path" DataField="ATTACHMENT_PATH">
                                    <HeaderStyle CssClass="hiddencol" />
                                    <ItemStyle CssClass="hiddencol" />
                                </asp:BoundField>
                                <asp:ButtonField ControlStyle-BackColor="#C6304A" ControlStyle-ForeColor="White" ButtonType="Button"
                                    CommandName="download" HeaderText="Download File(s)" ShowHeader="True" Text="Download">
                                    <ItemStyle ForeColor="#CC0000" HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ControlStyle BackColor="#C6304A" ForeColor="White"></ControlStyle>
                                    <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="15%" />
                                </asp:ButtonField>
                                <asp:CommandField ShowDeleteButton="True" HeaderText="Action">
                                    <HeaderStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
                                    <ItemStyle ForeColor="#CC0000" HorizontalAlign="Center" VerticalAlign="Middle" />
                                </asp:CommandField>
                                 <asp:BoundField HeaderText="EmpCode" DataField="emp_code">
                                    <HeaderStyle CssClass="hiddencol" />
                                    <ItemStyle CssClass="hiddencol" />
                                </asp:BoundField>
                            </Columns>
                            <HeaderStyle BackColor="#C6304A" ForeColor="White" />
                        </asp:GridView>

这是网格绑定代码:

  private void BindAttachmentGrid(string RequestNo)
    {        
        Grid_Attachments.DataSource = attachments.getAllAttachmentForARequest(RequestNo);
        Grid_Attachments.DataBind();
    }

更新:

我已经尝试过此代码,但是它仅适用于PDF文件

Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename="" + file.Name + """);
                Response.AddHeader("Content-Length", file.Length.ToString());
                string filetype = file.Extension;
                Response.ContentType = "application/"+filetype;
                Response.Flush();
                Response.TransmitFile(file.FullName);
                Response.End();

我想建议您访问此链接:

https://bugs.chromium.org/p/chromium/issues/detail?id=103618http://www.w3.org/protocols/rfc2616/rfc2616-sec19.html

尝试此代码::

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename="" + file.Name + """);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = your content type
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();

最新更新