javascript - How to upload file using ajax/jQuery with Symfony2 -


could me?

i'm trying write script when user clicks image, triggers image in database updated.

for wrote code temporarily makes caller line of method in controller, when send form not validated because of cross-site-request-forgery.

$("#upload_picture").on('click', function (e) {     e.preventdefault();     $("#bundle_user_file").trigger('click'); });  $("#bundle_user_file").change(function () {     if (this.files && this.files[0]) {         var reader = new filereader();          reader.onload = function (e) {             $('.active-img').attr('src', e.target.result);         };         reader.readasdataurl(this.files[0]);         ajax_formdata()     } }); 

this caller line ajax, treatment in form formdata post, caught routes , token. calls route, not sure if image going or not, inspector firefox.

function ajax_formdata() {      var @ = $("form[name=bundle_user]");      var formdata = new formdata();      formdata.append('file', $("input[type=file]")[0].files[0]);      var url = at.attr('action') + '?_token=' + $("#bundle_user__token").val();      $.ajax({          type: "put",          url: url,          data: formdata,          success: function (data) {              alert("success: " + data.message);          },          fail: function (data) {              alert("error: " + data.message);          },          cache: false,          contenttype: false,          processdata: false,          xhr: function () {  // custom xmlhttprequest              var myxhr = $.ajaxsettings.xhr();              if (myxhr.upload) { // avalia se tem suporte         propriedade upload                  myxhr.upload.addeventlistener('progress', function ()               {                  /* faz alguma coisa durante o progresso upload */              }, false);          }          return myxhr;      }  }); 

}

this method in controlodor common call click button submit change image. said before ajax call, replied token not available

public function updateaction(request $request, $id)     {         $this->denyaccessunlessgranted('role_user', null, 'unable access page!');          $em = $this->getdoctrine()->getmanager();          $entity = $this->getuser();          if ($entity->getid() != $id) {             $response = new jsonresponse(             array(                 'message' => 'não tem permissao'             ), 400);          return $response;         }          $form_update = $this->updateform($entity);         $form_update->handlerequest($request);          if ($form_update->isvalid()) {             $entity->upload();             $em->persist($entity);             $em->flush();              return new jsonresponse(array('message' => 'success!'), 200);         }          $response = new jsonresponse(             array(                 'message' => $form_update->geterrors()             ), 400);          return $response;     } 

firstly, notice click event #upload_image fires click trigger on #bundle_user_file, below asking change event. therefore, nothing.

you can re-generate csrf token if want calling csrf token_manager service doing this:

/** @var \symfony\component\security\csrf\csrftokenmanagerinterface $csrf */ $csrf = $this->get('security.csrf.token_manager'); $token = $csrf->refreshtoken($tokenid);  return new response($token); 

you can determine $tokenid in form, if want, or use picture id, or whatever. csrf token generated automatically session, might want check too.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -