【问题标题】:downloading multiple files using Ihttphandler使用 Ihttphandler 下载多个文件
【发布时间】:2010-10-20 21:56:26
【问题描述】:

太棒了!

我正在编写一个报告脚本,该脚本在单击按钮时会运行许多报告 (pdf)。报告是在网络服务器上创建的,然后我希望用户可以选择下载文件。我已经制定了从服务器下载一个文件的脚本。但我不确定如何下载多个文件? (大概有 50 个)

运行一份报告后,我将用户重定向到 http 处理程序脚本。

Response.Redirect("Download.ashx?ReportName=" + "WeeklySummary.pdf");

public class Download : IHttpHandler {


public void ProcessRequest(HttpContext context)
{


   StringBuilder sbSavePath = new StringBuilder();
   sbSavePath.Append(DateTime.Now.Day);
   sbSavePath.Append("-");
   sbSavePath.Append(DateTime.Now.Month);
   sbSavePath.Append("-");
   sbSavePath.Append(DateTime.Now.Year);

    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpResponse objResponce = context.Response;
    String test = HttpContext.Current.Request.QueryString["ReportName"];
    HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + test);
    objResponce.WriteFile(context.Server.MapPath(@"Reports\" + sbSavePath + @"\" + test));    
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.End();

}
public bool IsReusable { get { return false; } } 

}

提前致谢,如果您想看更多我的剧本,请告诉我。

【问题讨论】:

    标签: c# asp.net ihttphandler


    【解决方案1】:

    我马上看到的 2 个选项是简单地重复调用 HTTP 处理程序的明显选项。另一种方法是将它们压缩到服务器上并通过网络发送一个压缩文件。您可以使用内置的GZipStream 类来完成此操作。

    此外,您还需要在处理程序中添加一些代码,以便在下载这些临时文件后对其进行清理。

    【讨论】:

    • 我曾考虑过多次调用 HTTP Headler 负载,但不确定是否有更好的方法来做到这一点。想我会把它们拉上拉链。感谢您的链接!
    猜你喜欢
    • 2015-03-08
    • 2015-10-16
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多