ReadTextAsync in Windows Store app with multibyte file and JavaScript -
i read csv file in javascript written windows store app. if use readtextasync error when using german umlauts.
no mapping unicode character exists in target multi-byte code page.
i found solution in c# here readtext file in ansii encoding have no idea how solve in javascript?
is there someting encoding class in javascript? if convert file utf8 works fine, customer use file saved excel. excel not use utf8 default.
the simplest convert file utf-8 that's accessed winjs.
there isn't equivalent code in winjs library found c# (for reason, it's not exposed). there simple encodings available utf-8/16(l/h).
so, if don't want convert file , you're using javascript, i'd create simple windows runtime component (walkthrough) contains behavior want. it's quite simple if follow walkthrough. basically, write code in c#, , when done per rules, becomes available in winjs component. code you'd need write relatively straightforward well:
public sealed class winjsencodingextension { public iasyncoperation<string> readtextwithencodingasync(string appuri, string encodingname) { return readtextwithencodingasyncinternal(appuri, encodingname).asasyncoperation(); } private async task<string> readtextwithencodingasyncinternal(string appuri, string encodingname) { storagefile file = await storagefile.getfilefromapplicationuriasync( new uri(appuri, urikind.absolute)); var buffer = await fileio.readbufferasync(file); byte [] rawbytes = new byte[buffer.length]; using (var reader = datareader.frombuffer(buffer)) { reader.readbytes(rawbytes); } var encoding = encoding.getencoding(encodingname); return encoding.getstring(rawbytes, 0, rawbytes.length); } }
as task
class isn't available in winjs, returns interface iasyncoperation<t>
automatically wrapped promise
in winjs.
i've tested tiny bit, should on way:
var ex = new encodercomponent.winjsencodingextension(); var ex = ex.readtextwithencodingasync("ms-appx:///test1.txt", "utf-8") .then(function (result) { console.log(result); });
valid encodings in table on this page.
Comments
Post a Comment