2010-01-09 62 views
1

我有这个如何使用jQuery呈现响应流?

MemoryStream export = new MemoryStream(); 
    iCalendarSerializer serializer = new iCalendarSerializer(iCal); 
    serializer.Serialize(export,System.Text.Encoding.UTF8); 
    return export; 

,所以我使用C#DDay.iCal库导出我的日历。 Serialize接受一个“流”,所以我传递了一个内存流。

我现在有一个通用的处理程序,它调用包含上述代码的方法。

public class CalendarHandler : IHttpHandler 
    { 
     private Calendar service; 
     private Membership membershipS; 

     public void ProcessRequest(HttpContext context) 
     { 
      service = new Calendar(); 
      membershipS = new Membership (null); 
      string userName = context.User.Identity.Name; 
      Guid userId = membershipS.GetUsersId(userName); 

      context.Response.ContentType = "text/calendar"; 
      // calls the export calendar(the code that showed above that uses dDay ical. 
      var t = service.ExportCalendar(userId); 

      t.WriteTo(context.Response.OutputStream); 

     } 

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

所以现在我写icalendar到输出流。现在我有一个jQuery发布到这个方法,现在我不知道如何获取jQuery文章的OutputStream结果,并使用弹出的保存对话框。

$('#ExportCalendar').click(function(e) 
{ 
    $.post('../Models/CalendarHandler.ashx', null, function(r) 
    { 

    }); 

    return false; 
}); 

回答

1

我不认为一个AJAX文章可以导致文件保存对话框显示。这是因为AJAX帖子是以编程方式制作Web请求并发生在幕后(即,没有用户的知识)。

尝试改变链接是一个常规(非AJAX)的链接,如:您还需要设置内容部署标头得到很好的保存文件对话框

<a href="CalendarHandler.ashx">Save Calendar</a> 

。请参阅此链接的详细信息:

http://www.hanselman.com/blog/TheContentDispositionSagaControllingTheSuggestedFileNameInTheBrowsersSaveAsDialog.aspx

+0

@Eilon +1你对这种情况是正确的。我构建了一个小的JS/Flash库,它*可以*允许这样做,但它的目的是用于生成*和*保存文件在客户端。不要在服务器上生成文件,然后将其保存在客户端上:http://downloadify.info – 2010-01-09 02:21:24

0

无法通过AJAX使文件对话框弹出。但是,您可以这样做:

document.location = yourRequestUrl 

并且会生成一个对话框。如果你真的需要它是一个后,使用

$(this).parent("form").submit() 

为你的点击处理程序。

确保将context.Response.ContentType设置为“text/ical”。这告诉浏览器如何处理响应。