php - How to POST blob as a file using javascript? -


i have form, contents 2 inputs 1 hidden name="action" , second name="avatar" type="file". after user choose file in avatar input image converts blob. how can post blob loaded file , after submited?

form:

<form enctype="multipart/form-data" method="post" id="avatarform">     <input type="hidden" name="action" value="save" />     <input id="coolbutton" name="avatar" type="file" accept="image/*" class="form-control" placeholder=""> </form> 

i have try:

var fd = new formdata(); fd.append('action', 'save'); fd.append('avatar', file.name); fd.append('data', blob);  $.ajax({     type: 'post',     url: '/',     data: fd,     processdata: false,     contenttype: false }).done(function(data) {        console.log(data); }); 

php controller:

    if ($_files['avatar']['name']) {         if ($profile->avatar==1) {             unlink('/images/avatars/'.$profile->avatar);             $profile->avatar=0;         }         $filename="avatar_".$user_id.'.'.pathinfo($_files['avatar']['name'], pathinfo_extension);         move_uploaded_file($_files['avatar']['tmp_name'],$_server['document_root'] ."/images/avatars/".$filename);         $profile->avatar=$filename;     }    

but php controller not save it. if use form without script wich converts image blob - works , save file.

i have updated script

fd.append('blob', blob.replace("data:image/png;base64,", "") fd.append('ext', "jpg") // send ext of uploaded file 

and php controller

    if ($_post['blob']) {         if ($profile->avatar==1) {             unlink('/images/avatars/'.$profile->avatar);             $profile->avatar=0;         }         $file = base64_decode($_post['blob']);         $filename="avatar_".$user_id.'.'.$_post['ext'];         file_put_contents($_server['document_root'] ."/images/avatars/".$filename, $file);         $profile->avatar=$filename;     } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -