ReadTextAsync in Windows Store app with multibyte

2019-08-13 00:56发布

问题:

I would like to read a CSV file in my javascript written windows store app. If I use readTextAsync I get an error when using some german umlauts.

No mapping for the Unicode character exists in the target multi-byte code page.

I found a solution in C# here ReadText from file in ANSII encoding but I have no idea how to solve it in JavaScript?

Is there someting like the Encoding class in JavaScript? If I convert the file to UTF8 it works fine, but my customer would like to use a file saved by excel. Excel does not use UTF8 by default.

回答1:

The simplest would be to convert the file to UTF-8 as that's easily accessed by WinJS.

There isn't an equivalent to the code exactly in the WinJS library that you found for C# (for some reason, it's not exposed). There are only simple encodings available for UTF-8/16(L/H).

So, if you don't want to convert the file and you're using JavaScript, I'd create a simple Windows Runtime Component (walkthrough) that contains the behavior you want. It's quite simple if you follow the walkthrough. Basically, you write some code in C#, and when done per the rules, it becomes available in WinJS as a component. The code you'd need to write is relatively straightforward as 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 the Task class isn't available in WinJS, it returns an interface IAsyncOperation<T> that is automatically wrapped by a Promise in WinJS.

I've only tested it a tiny bit, but it should help you on your way:

var ex = new EncoderComponent.WinJSEncodingExtension();
var ex = ex.readTextWithEncodingAsync("ms-appx:///test1.txt", "UTF-8")
         .then(function (result) {
    console.log(result);    
});

Valid encodings are in the table on this page.