close

原本參考《透過Javascript觸發檔案下載》寫的下載檔案功能,會透過 JavaScript 在頁面內容塞 iframe,觸發瀏覽器的下載事件。

但在 IE 會出現錯誤訊息:

因為X-Frame-Options而出現錯誤訊息

  • 中文版錯誤訊息:
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/forbidframing.htm 的危害
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/ErrorPageTemplate.css 的危害
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/errorPageStrings.js 的危害
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/httpErrorPagesScripts.js 的危害
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/red_x.png 的危害
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/bullet.png 的危害
    SEC7111: HTTPS 安全性受到 by res://ieframe.dll/background_gradient.jpg 的危害
  • 英文版錯誤訊息:
    SEC7111: HTTPS security is compromised by res://ieframe.dll/forbidframing.htm
    SEC7111: HTTPS security is compromised by res://ieframe.dll/ErrorPageTemplate.css
    SEC7111: HTTPS security is compromised by res://ieframe.dll/errorPageStrings.js
    SEC7111: HTTPS security is compromised by res://ieframe.dll/httpErrorPagesScripts.js
    SEC7111: HTTPS security is compromised by res://ieframe.dll/red_x.png
    SEC7111: HTTPS security is compromised by res://ieframe.dll/bullet.png
    SEC7111: HTTPS security is compromised by res://ieframe.dll/background_gradient.jpg

 

上網查了一下,是因為 Apache 基於安全性而設定了 X-Frame-Options,例如 "X-Frame-Options: deny",或是以我們的設定為例,設定了 "X-Frame-Options: SAMEORIGIN",所以子網域之間就無法互相使用 iframe。

後來改成這樣寫:

// trigger download
var downloadLink = "https://test/something.exe";
if (window.document.documentMode) {
    // IE
    window.location.href = downloadLink ;
}
else {
    // Chrome, Firefox
    var $ifrm = $("<iframe style='display:none' />");
    $ifrm.attr("src", downloadLink );
    $ifrm.appendTo("body");
}

目前看起來基本上是可以執行,不過當檔案不存在(回應 HTTP 404)時,iframe 的作法看起來會毫無反應,要再來加強一下。

 

arrow
arrow
    全站熱搜

    小攻城師 發表在 痞客邦 留言(0) 人氣()