現象:IE8 下 window.open("downloadServlet") 提示「無法下載」,Chrome/360 正常。Java 後端設置 Content-Disposition: inline; filename=xxx.pdf。
window.open 為彈窗,若響應頭包含 Content-Disposition: attachment 或非文本類型,會觸發安全攔截或無法處理流。inline 模式下 IE8 嘗試在瀏覽器內渲染 PDF,若無插件則失敗。<a> 標籤模擬點擊下載<a id="downloader" href="downloadServlet" download="report.pdf" style="display:none"></a>
<script>
document.getElementById('downloader').click(); // 觸發下載
</script>
download 屬性指定下載文件名 (HTML5,IE10+ 支持)。download 屬性,但 a.click() 觸發導航下載機制,配合後端 Content-Disposition: attachment; filename="report.pdf" 可正常彈出「保存對話框」。response.setHeader("Content-Disposition", "attachment; filename=\"" + Id + ".pdf\"");
// 必須用 attachment,inline 會導致 IE8 嘗試內嵌顯示
response.setContentType("application/pdf");
response.setContentLength(abyte1.length);
response.getOutputStream().write(abyte1);
response.getOutputStream().flush();
window.open,改用隱藏 <a> 模擬點擊。Content-Disposition: attachment。IE8 兼容性差是歷史包袱,a.click() 是處理文件下載最穩健的跨瀏覽器方案。