使用webForm名称以.aspx扩展名下载文件

时间:2016-11-25 06:36:15

标签: c# asp.net

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

使用删除和下载按钮精确填充网格文件。但是当用户从谷歌浏览器下载文件时,文件名始终为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();

1 个答案:

答案 0 :(得分:0)

我建议您访问此链接:

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

尝试使用此代码::

public Dialog getCustomPogressDialog(Context context, String heading, String text) {

    // Declare the customer dialog
    Dialog dlgProgress = new Dialog(context);

    //  Set no title for the dialog
    dlgProgress.requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Set the content view to the customer_alert layout
    dlgProgress.setContentView(R.layout.layout_custom_process_progress);

    // Cancel the dialog when touched outside.
    dlgProgress.setCanceledOnTouchOutside(false);

    // Set the main heading
    TextView dlgHeading = (TextView) dlgProgress.findViewById(R.id.layCustomProgressHeading);
    dlgHeading.setText(heading);

    // set the info
    TextView dlgInfo = (TextView) dlgProgress.findViewById(R.id.layCustomProgressInfo);
    dlgInfo.setText(text);

    // Return the refenrece to the dialog
    return dlgProgress;

}