我已经为了解决这个问题而苦思冥想了几个小时。
我有一个包含下载文件的iframe。一切都很好,但是如果我的Response.ContentType = "APPLICATION/OCTET-STREAM";
,则不会调用iframe onload事件。
我的javascript函数如下:
function DownloadStuff(){
var DownloadSource = "http://Apage.aspx"
var iframe = $("#hiddenDownloader");
if (iframe.attr("id") === undefined) {
$('<iframe />', { id: 'hiddenDownloader', onload:'javascript:alertReady();' }).appendTo('body');
iframe = $("#hiddenDownloader");
}
iframe.attr('src', DownloadSource);
}
function alertReady() {
alert("Ready");
}
我的服务器端代码如下:
Response.Clear();
Response.CacheControl = "no-cache";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetValidUntilExpires(false);
Response.ExpiresAbsolute = DateTime.Parse("1/1/2000");
Response.Expires = 0;
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader("Content-Disposition", "attachment;filename=\"ReportDeck.pptx\"");
byte[] bytes = (byte[])PowerPointFile.Tables[0].Rows[0]["PPTBlob"];
bytes = Compression.DeCompressByteArray(bytes);
Response.OutputStream.Write(bytes, 0, bytes.Length);
如果我删除ContentType和Header,那么onload会被调用,但是文件将不会通过保存文件对话框下载,而是写入iframe中。
非常感谢您的帮助。
祝好, 拜伦·科布。