Commits

Mathias Panzenböck  committed 3e96445

export function for old IE versions using document.execCommand("SaveAs", ...)

  • Participants
  • Parent commits ea236c7

Comments (0)

Files changed (1)

File javascripts/magnatune.js

 };
 
 var DOWNLOAD_MIME_TYPE_MAP = {
-	"m3u":  "audio/mpegurl",
-	"xspf": "application/xspf+xml",
-	"json": "application/octet-stream",
-	"html": "application/octet-stream",
-	"pls":  "audio/x-scpls"
+	"audio/mpegurl":        "audio/mpegurl",
+	"application/xspf+xml": "application/xspf+xml",
+	"application/json":     "application/octet-stream",
+	"text/html":            "application/octet-stream",
+	"audio/x-scpls":        "audio/x-scpls"
 };
 
 var XML_CHAR_MAP = {
 else if ((ConstructibleBlob || BlobBuilder) && URL) {
 	// WebKit and Gecko
 	showSave = function (data, name, mimetype) {
-		var blob = makeBlob(data, mimetype);
-		var url = URL.createObjectURL(blob);
+		var blob, url;
 		if (DownloadAttributeSupport) {
+			blob = makeBlob(data, mimetype);
+			url = URL.createObjectURL(blob);
 			// currently only Chrome
 			var link = tag("a",{href:url,download:name||"Download.bin"});
 			var event = document.createEvent('MouseEvents');
 			link.dispatchEvent(event);
 		}
 		else {
+			blob = makeBlob(data, DOWNLOAD_MIME_TYPE_MAP[mimetype]);
+			url = URL.createObjectURL(blob);
 			window.open(url, '_blank', '');
 		}
 		setTimeout(function () {
 		}, 250);
 	};
 }
-else if (!$.browser.msie) {
+else if ($.browser.msie) {
 	// IE has problems handling data URLs
 	showSave = function (data, name, mimetype) {
-		window.open("data:"+(mimetype||"application/octet-stream")+","+encodeURIComponent(data),'_blank','');
+		var win = window.open("about:blank","_blank");
+		var doc = win.document;
+		if (!mimetype || mimetype.split(";")[0] !== "text/html") {
+			mimetype = "text/plain";
+			name += ".txt";
+		}
+		doc.open(mimetype,"replace");
+		doc.charset = "utf-8";
+		doc.write(data);
+		doc.close();
+		doc.execCommand("SaveAs",null,name);
+		win.close();
+	}
+}
+else {
+	showSave = function (data, name, mimetype) {
+		window.open("data:"+(DOWNLOAD_MIME_TYPE_MAP[mimetype]||"application/octet-stream")+","+encodeURIComponent(data),'_blank','');
 	};
 }
 
 			showSave(
 				this.exportPlaylist(playlist, opts),
 				filename,
-				DOWNLOAD_MIME_TYPE_MAP[opts.playlist_format]);
+				MIME_TYPE_MAP[opts.playlist_format]);
 		},
 		showExportSaved: function () {
 			this.hideExportMenu();