none
Getting "JavaScript runtime error: No mapping for the Unicode character exists in the target multi-byte code page." Error when reading certain JSON files

    คำถาม

  • I'm getting the error:  
      JavaScript runtime error: No mapping for the Unicode character exists in the target multi-byte code page.
    When I read certain JSON files from my local directory.  I have code as follows that does work for other json files.  How can I read this file that apparently has multi-byte data in it?
        function ReadAllDataFile(fileNameInLocalTree) {
            fileNameInLocalTree = "\data\\legislators.getList.json";
    
            var packagex = Windows.ApplicationModel.Package.current;
            var installedLocation = packagex.installedLocation;
            //var output = ["Installed Location: " + installedLocation.path];
            installedLocation.createFileAsync(fileNameInLocalTree, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    if (size == 0) {
                        // Data not found
                    }
                    else {
                        var inputStream = stream.getInputStreamAt(0);
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);
    
                        reader.loadAsync(size).then(function () {
                            var contents = reader.readString(size);
                         }
                    }
                })
            });
        }
    



    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    3 มกราคม 2555 19:28

คำตอบ

  • The error means what it sounds like:  There is some data that cannot be represented as a string in your code page.

    You should read it as binary data and then do with it what you need to.  Of course if you need it as a string, you will not be able to use that particular file (perhaps it is corrupt).  You could either use the readBuffer or readBytes methods on the DataReader for binary data.

    This sample uses the byte method: http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61

                                // Get the stream starting from byte 0.
                                imageStream = operation.getResultStreamAt(0);
                                
                                // Convert the stream to MS-Stream.
                                //var msStream = msWWA.createStreamFromInputStream(contentType, imageStream);
                                
                                var reader = new Windows.Storage.Streams.DataReader(imageStream);
                                
                                reader.loadAsync(size).then(
                                function (readsize) {
                                    var array = new Array(readsize);
                                    //                                array = reader.readBuffer(size);
                                    reader.readBytes(array);
    

     -Jeff

     

     

     


    Jeff Sanders (MSFT)
    • ทำเครื่องหมายเป็นคำตอบโดย Peter KellnerMVP 3 มกราคม 2555 21:26
    3 มกราคม 2555 19:53

ตอบทั้งหมด