javascript - convert blob js to file image php -
i try send datauri image using xhr , formdata() , find method tho convert datauri blob
function datauritoblob(datauri) { // convert base64/urlencoded data component raw binary data held in string var bytestring; if (datauri.split(',')[0].indexof('base64') >= 0) bytestring = atob(datauri.split(',')[1]); else bytestring = unescape(datauri.split(',')[1]); // separate out mime component var mimestring = datauri.split(',')[0].split(':')[1].split(';')[0]; // write bytes of string typed array var ia = new uint8array(bytestring.length); (var = 0; < bytestring.length; i++) { ia[i] = bytestring.charcodeat(i); }
and send blob var using xhr
var blob = datauritoblob(dataurl); var data = new formdata(); data.append('photos[]', blob, "file.png"); var xhr = new xmlhttprequest(); xhr.open('post', '/upload', true); xhr.upload.onprogress = function(e) { if (e.lengthcomputable) { var percentcomplete = (e.loaded / e.total) * 100; console.log(percentcomplete + '% uploaded'); } }; xhr.onload = function() { alert(this.response); var resp = json.parse(this.response); console.log('server got:', resp); }; xhr.send(data);
next , recive var photos[] , blob object , cant save file
function storeblob($blob, $filename) { $blobre = '/^data:((\w+)\/(\w+));base64,(.*)$/'; if (preg_match($blobre, $blob, $m)) { $imagename = (preg_match('/\.\w{2,4}$/', $filename) ? $filename : sprintf('%s.%s', $filename, $m[3])); return file_put_contents($imagename,base64_decode($m[4])); } return false; // error }
Comments
Post a Comment