【问题标题】:File downloading in .aspx extension with webForm name使用 webForm 名称以 .aspx 扩展名下载文件
【发布时间】:2016-11-25 15:15:31
【问题描述】:

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

使用删除和 下载 按钮可以很好地填充网格中的文件。但是当用户从 google chrome 下载文件时,文件名始终是 ViewRequest.aspx 这是页面名称。然后用户必须通过打开机制以合适的格式打开所选文件,例如用于 pdf 文件的 PDF 阅读器等。

Internet Explorer 中不会出现此问题。文件名和Grid中的文件名一样,扩展名也可以。

我不知道可能是什么问题。

谁能帮帮我。

这是我下载文件的代码

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

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我想建议您访问以下链接:

    https://bugs.chromium.org/p/chromium/issues/detail?id=103618 http://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();
    

    【讨论】:

    • 什么都没有发生,回发发生
    • 能否请您也粘贴您的设计页面代码?你用过更新面板吗?
    • 我已经添加了代码,我没有在这个页面的任何地方使用更新面板
    • 访问我的回答中提供的链接:还要确保您使用正确的内容类型的不同文件类型
    • 我无法尝试不同的文件类型,因为我不知道用户将上传哪种类型的文件。这取决于请求者。我正在使用 Response.ContentType = "application/octet-stream";
    猜你喜欢
    • 2020-01-03
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2012-10-17
    相关资源
    最近更新 更多