0

I want to send files via Ajax to the Server (upload) because of several benefits: no size limits, option to encrypt the data before sending it, combine data to 1MB pakages (slice one bigger file or combine several smaller files to one 1 MB pakage), (later) interrupt uploads and continue later. I've tried everything but whenever I find solutions in the web I allways have the problem that the data is incorrectly converted to utf8. Even "readAsDataUrl()" converts it incorrectly to utf8. As I know in the Mozilla developer website it is described, that this should create raw data which is just translated to base64. This is not true as I see. Using "readAsArrayBuffer()" does not help too, when the data is translated via "Uint8Array" to a string which now can be encrypted. And "readAsBinaryString()" is deprecated. There are several samples of code also on stackoverflow (ie Migrate FileReader ReadAsBinaryString() to ReadAsArrayBuffer() or ReadAsText()).

Maybe the issue is not in JavaScript but in PHP??? In one try I created a String from a blob (sliced file) via Int8Array and sent it to the server. In the PHP file I tried to create a file using the function "pack". It was also not encoded correctly but maybe there is an option to translate it to the right encoding??

Example: The Int8Array function (Int8Array(evt.target.result)) gave me the following data string: 80,75,3,4,20,0,0,8,0,0,-80,-109,73,79,94,-58,50 etc.

In PHP I tried to translate it in that way:

    $tst_str = "80,75,3,4...";
    $dec_arr = explode(",", $tst_str);
    $bin_str = "";
    foreach($dec_arr as $num) {
      $bin_str .= pack("c", $num);
    }
    $fp = fopen("testfile.php", "w+");
    fwrite($fp, $bin_str);
    fclose($fp);

Is it possible to get the real raw data and be able to write 1:1 back in a file, without taking care about the encoding? AS I know in PHP it is possible. You just have to open the file via the read option "rb" and write it with the option "wb", that's it...

  • don't use the FileReader, slice the file using `file.slice(start, end)` and upload that chunk as raw binary using the chunk as body or append it into a FormData – Endless May 30 '20 at 15:31
  • I'm not 100% sure, but I've solved the issue by using a self programmed base64encode function. That shows me that the issue is only in the delivered javascript function(s) to en- & decode b64 and not in the int8Array function. In other words: The data is loaded correctly or to be more precise int8Array (not Uint8Array) (and String.fromCharCode) is converting the buffer Array coming from 'readAsArrayBuffer' correctly, only 'btoa' is causing issues. In all solutions I found Uint8array was used, what seemed to solve the 'btoa' issues with special chars but converts the data itself incorrectly – Richard Jun 02 '20 at 05:30
  • The solution I have is very nice because it helps me to have the full upload process under control using ajax. It also helps me to upload big files, nearly with no (server) limitations. The important line of code is "base64encode(new Int8Array(evt.target.result).reduce((data, byte) => data + String.fromCharCode(byte), '''));". It's a code snippet I got from the net. I just changed 'btoa' to the personalized base64encoding function and 'Uint8Array' to 'Int8Array'. The b64-function I got from " phpeinfach" several years ago. – Richard Jun 02 '20 at 05:45

0 Answers0