Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
使用Ihttphandler下载多个文件 - 源码之家

使用Ihttphandler下载多个文件

问题描述:

Greatings!使用Ihttphandler下载多个文件

我正在制作一个报告脚本,它在按钮单击上运行多个报告(pdf)。这些报告是在Web服务器上创建的,然后我希望用户可以选择下载文件。我已经制定了从服务器下载一个文件的脚本。但我不知道如何下载多个文件? (可能会有大约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; } } 

} 

在此先感谢您,请让我知道您是否希望再看到我的脚本。

我马上看到的2个选项是显而易见的重复调用HTTP处理程序。另一种方法是在服务器上压缩它们,并通过电线发送zip文件。你可以使用内置的GZipStream类来实现这一点。

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

+0

我曾经想过只是调整HTTP Headler的负载时间,但不知道是否有更好的方法来做到这一点。想想我会压缩他们。感谢您的链接! – flyersun 2010-10-20 14:09:47