将生成的文件发送到客户端以在 ASP.NET 中下载

问题描述 投票:0回答:1

我正在尝试获取数据并生成文件,然后将文件发送到客户端进行下载。我使用

StreamWriter
写入文件内容,然后使用 Javascript 打开新窗口,但生成的文件不会发送到客户端。文件已经保存在路径中,只是文件没有导出到浏览器下载

protected void GenerateSummaryReport(int month, int year, string selmonth, string selYear)
{
    drpRepType.SelectedIndex = 1;
    string seconds = System.DateTime.Now.Second.ToString();

    DataTable dtAMName = CISEditor.Utility.Reports.GetAMNames(month, year);
    DataSet ds = CISEditor.Utility.Reports.GenerateSummaryReport(month, year);
        
    if (ds != null) 
    {
        if (ds.Tables["CIS"].Rows.Count > 0)
        {
            string physicalPath = @:F:\Report\";
            string filePath = "SummaryReport " + selMonth + " " + selYear + "" + seconds + ".xls";

            FileInfo fi = new FileInfo(filePath);

            if (fi.Exists)
            {
                fi.Delete();
            }

            StreamWriter sw = new StreamWriter(physicalPath + "\\Reports\\" + filePath);
            sw.Write(ExportToStringSummary(dtAMName, "FFFF6B, month , year));
            sw.Close();
            sw.Dispose();

            fi.Delete();
    
            StringBuilder strScripts= new StringBuilder();
            strScripts.Append("<script language=Javascript>");
            strScript.Append("window.open('" + physicalPath + "');");
            strScript.Append("</script>");

            RegisterClientScriptBlock("subscribescript", strScript.ToString());
        }   
        else
        {
            lblMsg.Text = "No records found!";
        }
    }   
}
c# asp.net webforms
1个回答
0
投票

为文件提供下载按钮相当容易。

但是,由于页面生命周期的工作方式,您只能将一个页面返回给客户端。

换句话说,按钮+下载文件=简单。

按钮 + 下载文件然后重定向到另一个页面?不!,既不容易也不可能。

下载多个文件也是如此。在大多数情况下,您必须压缩文件,然后为用户提供一次下载。

那么,说一下这个简单的网格视图:

<asp:GridView ID="GridFiles" runat="server"
    AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" Width="35%"
    CssClass="table table-hover" OnRowDataBound="GridFiles_RowDataBound">
    <Columns>
        <asp:BoundField DataField="FileName" HeaderText="FileName" />
        <asp:BoundField DataField="FileDate" HeaderText="UpLoaded" />
        <asp:BoundField DataField="FileSize" HeaderText="Size" />
        <asp:TemplateField HeaderText="Preview" ItemStyle-HorizontalAlign="Center" >
            <ItemTemplate>
                <asp:Image ID="imgPreview" runat="server" Height="80" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Download" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="cmdDownLoad"
                    runat="server" CssClass="btn"
                    OnClick="cmdDownLoad_Click"> 
                <span aria-hidden="true" class="glyphicon glyphicon-cloud-download"></span>
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="cmdDelete"
                    runat="server" CssClass="btn"
                    OnClick="cmdDelete_Click"> 
                <span aria-hidden="true" class="glyphicon glyphicon-trash"></span>
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在上面,我有一个下载文件的按钮,其代码是:

    protected void cmdDownLoad_Click(object sender, EventArgs e)
    {
        LinkButton myBut = (LinkButton)sender;
        GridViewRow gRow = (GridViewRow)myBut.NamingContainer; // get current grid row

        string strFile = gRow.Cells[0].Text;
        string strFullFile = Server.MapPath($"~/UpLoadFiles/{strFile}");
        string sMineType = MimeMapping.GetMimeMapping(strFile);
        Response.ContentType = sMineType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFile);
        Response.TransmitFile(strFullFile);
        Response.End();

    }

因此效果是这样的:

因此,您可以轻松使用 GridView,并提供要下载的文件列表。

注意使用“transmitFile”来下载文件。

但是,如上所述,您只能下载一个文件,而且页面上很难发生任何其他操作,因为您通常只允许浏览器对文件下载做出一个“响应”。

© www.soinside.com 2019 - 2024. All rights reserved.