如何通过JavaScript/jQuery实现IE/Edge下的.msg文件下载?
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-Typeheader: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.

