Search code examples
windows-8winjs

System.Text.Encoding in WinJS


Is there an equivalent of

System.Text.Encoding.UTF8.GetString(fileContent) 

in WinJS (Windows 8 Store App written in javascript/HTML)?

EDIT. fileContent is a byte array.


Solution

  • There is no strict equivalent of System.Text.Encoding.UTF8.GetString in WinJS, but you can try implement file reading to string as follows:

    file.openReadAsync().done(function (stream) {
       var blob = MSApp.createBlobFromRandomAccessStream(file.contentType, stream);
       var reader = new FileReader();
    
       reader.onload = function(event) {
          var fileAsText = event.target.result;
       };
    
       reader.readAsText(blob, 'UTF-8');
    });
    

    In most cases (file upload via XHR, displaying file) you don't need to have file contents as text, so just use Blob then.