0

When I assign a decoded ArrayBuffer to a dom.value and then I encode it again, I do not get the same ArrayBuffer. In the following example, A is "ArrayBuffer { byteLength: 394924 }" whereas B is "ArrayBuffer { byteLength: 585517 }" ???? Why ?

Fbx is Autodesk's 3D model file format. It is binary, not text. I want to load the model in a previewer before it gets uploaded, just locally on the client web browser site.

html

<input id="fileUploadInput" type="file" accept=".fbx"/>

Javascript

let _handleFileSelect = function ( event ) {

    let reader = new FileReader();
        
    let files = {... event.target.files};
    let file = files[0]; 
    reader.readAsArrayBuffer(file);
    reader.onload = (function(reader) {
        return function() {

            let fbxBuffer = reader.result;

            console.log("A", fbxBuffer);

            let dec = new TextDecoder();
            document.getElementById('fbxFileInput').value = dec.decode(fbxBuffer);


             let v = document.getElementById('fbxFileInput').value;

             let enc = new TextEncoder();   
  
            console.log('B', (enc.encode(v)).buffer);
        }
    }
        
        
var multipleFilesInputElem = document.getElementById( 'fileUploadInput' );
multipleFilesInputElem.addEventListener( 'change' , _handleFileSelect, false);

The question can be summarized as follows: "Why A and B are not the same?"

console.log('A', fbxBuffer);

console.log('B', (new TextEncoder().encode(new TextDecoder().decode(fbxBuffer))).buffer);

Some ArrayBuffer values are not the same but some are !

enter image description here

Dimitrios Ververidis
  • 1,118
  • 1
  • 9
  • 33
  • 1
    Because `TextDecoder` does not throw although your arrayBuffer contains invalid/malformed data. And on the other end `TextEncoder` does **not** produce malformed data. What are you trying to do here? why are you writing binary data into a text?-field. – Thomas Jul 22 '20 at 13:12
  • What exactly is the nature of the file itself? Is it text? – Pointy Jul 22 '20 at 13:13
  • @Pointy `accept=".obj,.mtl,.jpg,.png,.fbx"` unlikely – Thomas Jul 22 '20 at 13:14
  • I actually edited it. It is only for fbx files that are binary. Fbx is autodesk's 3D model file format. I want to load the model in a previewer before it gets uploaded, just locally on the client web browser site. – Dimitrios Ververidis Jul 22 '20 at 13:28
  • After some search I found that my question is much similar to https://stackoverflow.com/questions/47296601/textencoder-and-textdecoder-not-perfect-inverses-of-each-other – Dimitrios Ververidis Jul 22 '20 at 13:29
  • 1
    TextEncoder IS NOT the exact opposite of TextDecoder – Dimitrios Ververidis Jul 22 '20 at 13:30
  • 2
    Right, plus you can't expect a binary file format to be a valid sequence of UTF-8 codes. – Pointy Jul 22 '20 at 13:52

1 Answers1

0

It seems that the problem is by definition ill posed. As Pointy says some information is lost in decoding binary values to UTF-8, so it is not possible to reverse the UTF-8 to ArrayBuffer binary values again. The valid binary parts are only those that refer to text values.

Dimitrios Ververidis
  • 1,118
  • 1
  • 9
  • 33
  • *"some information is lost in decoding binary values to UTF-8"* no, it is more like me telling you to read this english text, and then I give you an excerpt from a chinese cookbook (in not english). And then I tell you to write down the words you've read and wonder why it's not the same as the original. – Thomas Jul 23 '20 at 06:34