changeset 96:2d4b56e4d966

Improve mpSaveBinaryFile() to also handle string format data. Also improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 06 Jul 2018 02:58:28 +0300
parents 517348a7b543
children 65e6cacd068c
files multipaint.pde
diffstat 1 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/multipaint.pde	Fri Jul 06 02:10:59 2018 +0300
+++ b/multipaint.pde	Fri Jul 06 02:58:28 2018 +0300
@@ -292,10 +292,27 @@
 // "Save" a byte array to file. Basically creates a blob URI
 // and dumps it in the DOM, giving user a download.
 //
-void mpSaveBinaryFile(String name, byte[] data)
+bool mpSaveBinaryFile(String name, byte[] data)
 {
-    var blob = new Blob([new Uint8Array(data)], {type: "application/octet-stream"}),
-        url = window.URL.createObjectURL(blob);
+    var blob = null;
+    if (typeof(data) == "string")
+        blob = new Blob([data], {type: "application/octet-stream"});
+    else
+    if (typeof(data) == "object")
+        blob = new Blob([new Uint8Array(data)], {type: "application/octet-stream"});
+
+    if (blob == null)
+    {
+        console.log("Could not create BLOB from data.");
+        return false;
+    }
+
+    var url = window.URL.createObjectURL(blob);
+    if (url == null)
+    {
+        console.log("Could not create URL from BLOB object.");
+        return false;
+    }
 
     var alink = stCE("a");
     var mpUI = stGE("mpUI");
@@ -308,6 +325,7 @@
     alink.click();
 
     window.URL.revokeObjectURL(url);
+    return true;
 }