You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JavaScript/jQuery实现IE/Edge下的.msg文件下载?

Fixing .msg File Downloads in IE/Edge (JavaScript/jQuery)

Got it, let's tackle this IE/Edge .msg download issue you're facing—super common pain point since these browsers handle file downloads way differently from Chrome. The problem is that IE/Edge tries to render .msg files as plain text instead of triggering a download prompt, which causes that messy garbled text you're seeing. Here's a tested, cross-browser solution using JavaScript/jQuery:

Core Approach

We'll detect if we're dealing with IE or legacy Edge (pre-Chromium) and use their native msSaveBlob/msSaveOrOpenBlob methods to handle the download. For Chrome and modern Edge, we can stick with your original location.href method.

Full Code Implementation

function downloadMsgFile(fileUrl, fileName) {
  // Check for IE/legacy Edge support for msSaveBlob
  const supportsIEMethods = !!window.navigator.msSaveBlob || !!window.navigator.msSaveOrOpenBlob;

  if (supportsIEMethods) {
    // Fetch the file as a Blob (binary data) using jQuery AJAX
    $.ajax({
      url: fileUrl,
      type: 'GET',
      xhrFields: {
        responseType: 'blob' // Critical: Ensures we get binary data instead of text
      },
      success: function(blob) {
        // Trigger IE/Edge's native save dialog
        // Use msSaveBlob to force save, or msSaveOrOpenBlob to let user choose open/save
        window.navigator.msSaveBlob(blob, fileName);
        // Alternate option: window.navigator.msSaveOrOpenBlob(blob, fileName);
      },
      error: function(xhr, status, err) {
        console.error('Download failed:', err);
        alert('Failed to download the .msg file. Please try again.');
      }
    });
  } else {
    // For Chrome, modern Edge, Firefox, etc.—use your original method
    window.location.href = fileUrl;
  }
}

// How to use it:
// downloadMsgFile('domain/path/filename.msg', 'your-custom-filename.msg');

Key Details to Note

  • Blob Response Type: Setting responseType: 'blob' in the AJAX request ensures we grab the raw binary data of the .msg file, which prevents the garbled text issue in IE/Edge.
  • Server MIME Type: Make sure your server returns the .msg file with the correct Content-Type header: application/vnd.ms-outlook. This helps browsers properly identify the file type, even if IE still needs the Blob workaround.
  • Cross-Domain Considerations: If your .msg files are hosted on a different domain, your server needs to enable CORS (Cross-Origin Resource Sharing) to allow the AJAX request to fetch the Blob.
  • Legacy vs Modern Edge: Modern Edge (Chromium-based) works fine with location.href, but our detection logic will still fall back to the Chrome method automatically—no extra work needed.

This solution should cover all your bases, letting users download .msg files seamlessly across Chrome, IE, and both legacy/modern Edge.

内容的提问来源于stack exchange,提问作者Thomas A.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:22:22