首页 > 解决方案 > Decode Javascript packed binary data at server PHP side

问题描述

I have HTML form with 2 controls - file input control #resume and submit button #action_cv. Most of files I can (and want) upload to server are binary. There is reading and packing (encoding) this data at front side (Javascript):

function readFile(file, callback) {
   let reader = new FileReader();
   reader.onload = callback;
   reader.readAsArrayBuffer(file);
}

$(document).ready(function () {

   $('#action_cv').on('submit', function (event) {
       let input_data = {
           resume: null,
           resume_data: null
       };
       let resume = null;
       if (($("#resume"))[0].files.length > 0) {
           resume = ($("#resume"))[0].files[0];
           input_data['resume'] = resume.name;
           readFile(resume, function (evt) {
               let data = evt.target.result;
               let bs =
                   String.fromCharCode.apply(null, new Uint8Array(data));
               input_data['resume_data'] = bs;
           });
       }
       // AJAX call with input_data skipped here...
   })
});

Here data contains raw binary data from file, and bs - already packed for AJAX submission to server written by PHP. My question is very simple - how to unpack this encoded (packed) binary in PHP to get original file at server side? (No need to provide writing file operation - it seems evident)

标签: javascriptphphtmljqueryajax

解决方案


推荐阅读