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

    Pertanyaan

  • 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
    03 Januari 2012 19:28

Jawaban

  • 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)
    03 Januari 2012 19:53
    Moderator

Semua Balasan

  • Hi Peter,

    What line of code is giving you this exception?

    -Jeff


    Jeff Sanders (MSFT)
    03 Januari 2012 19:39
    Moderator
  •  var contents = reader.readString(size);

    I took out the stuff I did below this line so it would not confuse the question.  I've tried other methods in the reader without success (that is, readBytes,readBuffer).  ReadBuffer seemed to have the most potential but it just returns a 2 value object with the length of the buffer as one.  I believe they are native WinRT calls so I can't debug to tell what is going on.

    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    03 Januari 2012 19:42
  • 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)
    03 Januari 2012 19:53
    Moderator
  • Thanks Jeff! I just did a post explaining everything you explained with working source.  Hopefully, i if Microsoft does not change all this when beta and production come a long, it might help someone else.

    http://peterkellner.net/2012/01/03/with-windows-8-metro-html5css3javascriptwinrt-read-any-file-from-project-directory/

     


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    03 Januari 2012 21:27