开发者

Generating file with ajax and allow user to download it?

I have very similar problem as this: Allowing users to download files - ASP.NET , but in my case I am generating xlsx file with ajax, and on ajax-called aspx page I am using:

Response.Clear();
Response.ContentType = "application/octet-stream";
string filename = User.Identity.Name + "_Report.xlsx";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response.WriteFile(AppDomain.CurrentDomain.BaseDirectory + "Reports\\" + filename);
Response.End();

When this file is generated, control is returned to ajax calling page and from there I wan't to show save file dialog based on this ajax response and allow user to download this generated file. I don't want to save file on disk with ajax called page and th开发者_StackOverflowen redirect ajax calling page to that file, because of popup blocker in IE. I am using jquery for ajax calls:

$.ajax({  
    type:"POST", 
    url: "AjaxReport.aspx",               
    data:dataString,
    success: function(data) {
        //don't want to use this  
        // $('#RedirectIFrame').attr('src','Reports/Report.xlsx?cache='+Math.random());
        //want to use data variable containing ajax response (bytes of Report file) showing
        //save dialog to download this file from browser
    }              
});   

How to do this?


Currently JavaScript can't access to the user's file system, meaning you can't prompt users to save a file coming from a network stream.

In other words. You'll need to do that redirect and write your file stream to the HTTP response and let the user decide what to do :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜