osx - Javascript -> Download CSV file encoded in ISO-8859-1 / Latin1 / Windows-1252 -


i have hacked small tool extract shipping data amazon csv order data. works far. here simple version js bin: http://output.jsbin.com/jarako

for printing stamps/shipping labels, need file uploading deutsche post , other parcel services. used small function savetextasfile found on stackoverflow. far. no wrong displayed special characters (äöüß...) in output textarea or downloaded files.

all these german post / parcel services sites accept latin1 / iso-8859-1 encoded files upload. downloaded file utf-8. if upload it, special characters (äöüß...) go wrong.

how can change this? still searched lot. have tried i.e.:

setting charset of tool iso-8859-1:

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 

but result is: have wrong special characters still in output textarea , in downloaded file. if upload post site, still more wrong characters. if check encoding in coda editor still says downloaded file utf-8.

the savetextasfile function uses var textfileasblob = new blob([texttowrite], {type:'text/plain'});. may there ways set charset download there!?

function savetextasfile() {     var texttowrite = $('#dataoutput').val();     var textfileasblob = new blob([texttowrite], {type:'text/plain'});     var filenametosaveas = "brief.txt";      var downloadlink = document.createelement("a");     downloadlink.download = filenametosaveas;     downloadlink.innerhtml = "download file";     if (window.webkiturl != null)     {         // chrome allows link clicked         // without adding dom.         downloadlink.href = window.webkiturl.createobjecturl(textfileasblob);     }     else     {         // firefox requires link added dom         // before can clicked.         downloadlink.href = window.url.createobjecturl(textfileasblob);         downloadlink.onclick = destroyclickedelement;         downloadlink.style.display = "none";         document.body.appendchild(downloadlink);     }      downloadlink.click(); } 

anyhow, there have way download files in other encoding site uses itself. amazon site, download csv file utf-8 encoded. downloaded csv file there latin1 (iso-8859-1) if check in coda...

scroll down update real solution!

because got no answer, have searched more , more. looks there no solution in javascript. every test download i'v made, generated in javascript utf-8 encoded. looks javascript made unicode / utf-8 or other encoding (possibly) apply if data transported again using former http transport. javascript, runs on client no additional http transport happens, because data still on client..

i have helped me building small php script on server, send data via or post request. converters encoding latin1 / iso-8859-1 , downloads file. iso-8859-1 file correctly encoded special characters, can upload mentioned postal , parcel service sites , looks good.

latin-download.php: (it important save php file in iso-8859-1, make work!!)

<?php $decoded_a = urldecode($_request["a"]); $converted_to_latin = mb_convert_encoding($decoded_a,'iso-8859-1', 'utf-8'); $filename = $_request["filename"]; header('content-disposition: attachment; filename="'.$filename.'"; content-type: text/plain; charset=iso-8859-1;'); echo $converted_to_latin; ?> 

in javascript code use:

<a id="downloadlink">download file</a>  <script> var mydata = "this testdata containing äöüß";  document.getelementbyid("downloadlink").addeventlistener("click", function() {     var mydatatosend = encodeuricomponent(mydata);     window.open("latin-download.php?a=" + mydatatosend + "&filename=letter-max.csv"); }, false); </script> 

for bigger amounts of data have switch post...

update 08-feb-2016

a half year later have found solution in pure javascript. using inexorabletash/text-encoding. polyfill encoding living standard. standard includes decoding of old encodings latin1 ("windows-1252"), forbids encoding these old encoding types. if use browser implemented window.textencoder function offer utf encoding. but, polyfill solution offers legacy mode, allow encoding old encodings latin1.

i use that:

<!doctype html> <script> // 'copy' browser build in textencoder function textencoderorg (because can not encode windows-1252, can still use textencoderorg()  ) var textencoderorg = window.textencoder;    // ... , deactivate it, make sure polyfill encoder script follows used  window.textencoder = null;    </script> <script src="lib/encoding-indexes.js"></script>  // needed support encode old encoding types <script src="lib/encoding.js"></script>  // encording polyfill  <script>  function download (content, filename, contenttype) {     if(!contenttype) contenttype = 'application/octet-stream';         var = document.createelement('a');         var blob = new blob([content], {'type':contenttype});         a.href = window.url.createobjecturl(blob);         a.download = filename;         a.click(); }  var text = "es wird ein schöner tag!";  // encoding var encoded = new textencoder("windows-1252",{ nonstandard_allowlegacyencoding: true }).encode(text);  // download 2 files see difference download(encoded,"windows-1252-encoded-text.txt"); download(text,"utf-8-original-text.txt");  </script> 

the encoding-indexes.js file 500kb big, because contains encoding tables. because need windows-1252 encoding, use have deleted other encodings in file. there 632 byte left.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -