使用stream + jquery + asp.net处理程序下载文件

时间:2013-06-04 19:00:22

标签: c# jquery asp.net c#-4.0 stream

我试图通过我的处理程序和jquery使用流下载文件,我有一个返回Stream的处理程序

public void ProcessRequest(HttpContext context){

    string name = "MyFile.xlsx"        

    context.Response.ContentType = "application/octet-stream";
    context.Response.AddHeader( "Content-Disposition", "attachment; filename=" + name ); 
    Stream stream = GetFileStream("id001");

    context.Response.Write (stream);
}

处理此处理程序的页面,但我无法映射响应

    function downloadFile(e) {
        var baseUrl = "./handlers/DownloadReport.ashx?";
        e.preventDefault();            
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));

        var key = "key=" + dataItem.Link;
        var name = "&name=" + dataItem.ReportName;
        $.get(baseUrl + key + name, function (result) {
            // Check for errors 

        });
    }

关于如何使这项工作的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为问题是jQuery要么没有获得流EOF,要么在流完成时关闭连接,因此javascript无法处理它。

您可以尝试使用jQuery流插件获取流,这可能会有所帮助: https://code.google.com/p/jquery-stream/wiki/API