# HG changeset patch # User Matti Hamalainen # Date 1530835108 -10800 # Node ID 2d4b56e4d9664d64766d2ff59b3e42f7d17323ba # Parent 517348a7b543a4e79d63bfbf7c9f82c9003dd057 Improve mpSaveBinaryFile() to also handle string format data. Also improve error handling. diff -r 517348a7b543 -r 2d4b56e4d966 multipaint.pde --- 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; }