WebPlotDigitizer
WebPlotDigitizer copied to clipboard
A better exporter
From 92086796ec6c8c90ac14a592fce4e35febc018e2 Mon Sep 17 00:00:00 2001
From: KOLANICH
Date: Fri, 9 Feb 2018 12:59:04 +0300
Subject: Made JS export better.
1 There is no need to append the link to body. It works without this.
2 Added MIME type selection.
3 Added revocation of the URI to free resources.
---
app/javascript/services/download.js | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/app/javascript/services/download.js b/app/javascript/services/download.js
index c91ed28..4e7657b 100644
--- a/app/javascript/services/download.js
+++ b/app/javascript/services/download.js
@@ -33,14 +33,20 @@ wpd.download = (function() {
}
}
+ const formatMimeMapping = {
+ "json":"application/json",
+ "tsv":"text/tab-separated-values",
+ "csv":"text/comma-separated-values"
+ };
function textFileLocal(data, filename) {
+ let nameParts=filename.split(".");
+ let ext=nameParts[nameParts.length - 1];
let $downloadElem = document.createElement('a');
- $downloadElem.href = URL.createObjectURL(new Blob([data]), {type:"text/plain"});
+ $downloadElem.href = URL.createObjectURL(new Blob([data]), {type:formatMimeMapping[ext.toLowerCase()]});
$downloadElem.download = stripIllegalCharacters(filename);
$downloadElem.style.display = "none";
- document.body.appendChild($downloadElem);
- $downloadElem.click();
- document.body.removeChild($downloadElem);
+ $downloadElem.dispatchEvent(new MouseEvent("click"));
+ URL.revokeObjectURL($downloadElem.href);
}
function textFileServer(data, filename) {
--
Thanks, I'll include this too
When are you going to apply it?
Please remove $downloadElem.style.display = "none";
too, I havs forgotten to remove it.
Please make pull requests for code changes in the future. I will merge your changes once I have some time to spare (today evening or tomorrow).