C# 下载文件时Iframe onload

C# 下载文件时Iframe onload,c#,javascript,jquery,iframe,download,C#,Javascript,Jquery,Iframe,Download,我花了好几个小时才弄明白这一点。 我有一个iframe,我想用它下载一个文件。这一切都可以正常工作,但是如果myResponse.ContentType=“APPLICATION/OCTET-STREAM” 我的javascript函数如下所示: function DownloadStuff(){ var DownloadSource = "http://Apage.aspx" var iframe = $("#hiddenDownloader"); if (if

我花了好几个小时才弄明白这一点。 我有一个iframe,我想用它下载一个文件。这一切都可以正常工作,但是如果my
Response.ContentType=“APPLICATION/OCTET-STREAM”

我的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

感谢您的帮助

问候,,
拜伦·科布。

嗯,是的
onload
仅在将文档加载到iframe时调用。保存操作不会将文档加载到框架中;旧文档保留在原位,并弹出“另存为”对话框

您无法检测到仅从JavaScript接受或完成了文件下载。如果您真的需要检测到这一点,那么最接近的方法就是让服务器端以唯一的ID存储下载进度,并为客户端脚本提供AJAX接口,以查询服务器是否已完成文件发送

顺便说一下:

onload:'javascript:alertReady();'

这是值得怀疑的。您不需要也不应该在事件处理程序属性中使用
javascript:
(这仅适用于
href=“javascript:…”
URL,也不应该使用它!)。将事件处理程序属性设置为JS中的字符串是非常危险的。直接传递
onload:alertReady
会更好,或者因为您使用的是jQuery,所以使用标准绑定方法,如
iframe.load(function(){…})

谢谢。我的onload是这样的,因为在我意识到这是因为contenttype之前,我尝试了所有可能的onload解决方案。嗯,我问了类似的问题