1

I have a FBX file generated from blender which I'm trying to pack with a zip for use in an application that uses THREE JS to render images in an AR environment for use in a Webapp. Right now I'm trying to upload it using javascript however the data of the result is always corrupted by a few bytes, rendering it completely useless. I am using HexFiend on MacOS 10.13.6 to inspect the files.

The file is a 1x1x1 cube in Blender which was exported using the 7.3 FBX Binary protocol. This is then loaded in from a Vue.js client to a Laravel / Php 7.0.31 backend. Don't mind the indentation for now - this is my latest attempt.

var decoder = new TextDecoder('utf-8');
var reader = new FileReader();
        reader.onload = (e) => {
            let asset = {};

            var result = new Uint8Array(e.target.result);
            var array = decoder.decode(result);

            asset.data = array;
            asset.filename = file.name;
            asset.size = file.size;
            asset.mime = file.type != '' ? file.type : "application/octet-stream";

            console.log(asset.data);

            //save it to an object that is then posted to the php side.
        }
reader.readAsArrayBuffer(file);

The files are generated, but always off by a few bits causing them to corrupt and become utterly useless. I need to keep the binary of the file as pure as possible. My suspicion is that the utf-8 encoding breaks it by transforming a few characters.

So far I have tried the following:

  1. readAsBinaryString: this resulted in equal artifacts and rendered the file useless after processing.
  2. readAsText: this, unsurprisingly, results in the same result as above.
  3. readAsDataURL: this creates an unknown mime type for the base64 instance which can't be used on the front end so is out of the question.
  4. readAsArrayBuffer injected directly into the object: Unfortunately is just a giant array of unorganized numbers
  5. Inject ',' or '.' between the arraybuffer to then 'decrypt' before being usable. Inflated the size by 2,5 times and didn't work! So not a good solution.
  6. cast the array to 16bit - again, would need to be 'decrypted' before being usable. So out of the question.
  7. Use String.fromCharCode.apply(null, array) to make the array. Results in similar as the above situation.

So far the only significant change I have seen is between readAsBinaryString and the above solution. But both are not correct.

Veraduxxz
  • 67
  • 9
  • Wait it is a binary File? Why do you even want to read it on front end? Send it directly as it is. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data – Kaiido Aug 22 '18 at 12:26
  • If you have a good reason you want to read it on the front-end then please [edit] your question stating why, and notice me of this edit (using @Kaiido) so I can reopen if needed. – Kaiido Aug 22 '18 at 12:31
  • I had explained in the question what the link is, but apparently it wasn't clear enough. There is a Client that links to the backend. This is where the file is uploaded / saved onto the server through Vue.js -> Laravel. It is put in a bundle that is then downloaded by the frontend webapp and opened to display in THREE.JS to render a AR model. It is not at all related to the question you have linked since this doesn't pertain to form data at all. The UTF-8 Encoding is breaking the binary array of the file after trying to save it. It is close - but not exact. That is my problem @Kaiido – Veraduxxz Aug 22 '18 at 13:26
  • But where does that UTF-8 encoding comes from? Why do you need to read it as text? – Kaiido Aug 22 '18 at 13:29
  • If you see the code - you can see where it comes from. I can't directly inject the. Arraybuffer into PHP because it will generate a file only reading the number of it. I need a string representation of the binary to inject into the file that PHP will generate. The UTF-8 Encoding comes from attempts to generate this file. – Veraduxxz Aug 22 '18 at 13:31
  • So the dupe target is correct: you don't want to send it as string to the php script. Send it as multipart through a FormData and receive it directly as a binary file in your php script. – Kaiido Aug 22 '18 at 13:33
  • Except that doesn't work because if you've read - we use Laravel. We synchronize an object to the models we have created. My issue is that there is sometimes an offset of at most 2 bits. – Veraduxxz Aug 22 '18 at 13:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178538/discussion-between-kaiido-and-veraduxxz). – Kaiido Aug 22 '18 at 13:35

0 Answers0