From 8bf314248ecf3273e9900c1ab04f7f85217b2127 Mon Sep 17 00:00:00 2001 From: Taka Date: Sat, 2 Oct 2021 11:23:52 +1000 Subject: [PATCH] Convert file downloads to UTF-8 when running in a browser. --- vasl_templates/webapp/static/lfa.js | 2 +- vasl_templates/webapp/static/scenario-upload.js | 2 +- vasl_templates/webapp/static/snippets.js | 2 +- vasl_templates/webapp/static/utils.js | 6 ++++++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/vasl_templates/webapp/static/lfa.js b/vasl_templates/webapp/static/lfa.js index 0d1d33f..8d24301 100644 --- a/vasl_templates/webapp/static/lfa.js +++ b/vasl_templates/webapp/static/lfa.js @@ -1890,7 +1890,7 @@ function onDownloadData() if ( gWebChannelHandler ) gWebChannelHandler.save_log_file_analysis( data ) ; else - download( data, "analysis.csv", "application/text" ) ; + download( toUTF8(data), "analysis.csv", "application/text" ) ; } } diff --git a/vasl_templates/webapp/static/scenario-upload.js b/vasl_templates/webapp/static/scenario-upload.js index 74b5573..23d06b3 100644 --- a/vasl_templates/webapp/static/scenario-upload.js +++ b/vasl_templates/webapp/static/scenario-upload.js @@ -275,7 +275,7 @@ function uploadFiles( asaScenarioId ) // prepare the upload var formData = new FormData() ; var vtSetupJson = JSON.stringify( vtSetup, null, 4 ) ; - var vtSetupJsonUTF8 = (new TextEncoder()).encode( vtSetupJson ) ; + var vtSetupJsonUTF8 = toUTF8( vtSetupJson ) ; formData.append( "vt_setup", makeBlob( vtSetupJsonUTF8, "application/json" ), prefix + "|" + "scenario.json" diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js index 78998d9..2f9a2d6 100644 --- a/vasl_templates/webapp/static/snippets.js +++ b/vasl_templates/webapp/static/snippets.js @@ -1842,7 +1842,7 @@ function on_save_scenario() } // return the parameters to the user as a downloadable file - download( data, save_fname, "application/json" ) ; + download( toUTF8(data), save_fname, "application/json" ) ; // NOTE: We get no indication if the download was successful, so we can't show feedback :-/ // Also, if the download didn't actually happen (e.g. because it was cancelled), then setting diff --git a/vasl_templates/webapp/static/utils.js b/vasl_templates/webapp/static/utils.js index bf0ae7e..ffbe284 100644 --- a/vasl_templates/webapp/static/utils.js +++ b/vasl_templates/webapp/static/utils.js @@ -498,6 +498,12 @@ function addUrlParam( url, param, val ) return url + "?" + param + "=" + encodeURIComponent(val) ; } +function toUTF8( val ) +{ + // convert the value to UTF-8 (nb: returns a Uint8Array) + return (new TextEncoder()).encode( val ) ; +} + function escapeHTML( val ) { return new Option(val).innerHTML ; } function trimString( val ) { return val ? val.trim() : val ; } function fpFmt( val, nDigits ) { return val.toFixed( nDigits ) ; }