php - Symfony2 upload javascript blob -
i'm trying upload image created canvas symfony using blob. javascript code working , sending blob. in controller can't pass validation. when seek read validation, doesn't contain errors.
is there problem in foto.php? or in controller?
javascript send post:
var dataurl = canvas.todataurl("image/png", 0.5); var blob = datauritoblob(dataurl); var formdata = new formdata(); formdata.append('file', blob); var xhr = new xmlhttprequest(); // add together event handlers here... xhr.open('post', '{{ path("foto_uploadwebcam" ) }}', true); xhr.send(formdata); function datauritoblob(datauri) { // convert base64/urlencoded info component raw binary info 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); } homecoming new blob([ia], {type:mimestring}); }
foto.php (partially)
/** * foto * * @orm\table() * @orm\entity(repositoryclass="yeouuu\fotobundle\entity\fotorepository") * @orm\haslifecyclecallbacks */ class foto { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @assert\file(maxsize="6000000") */ private $file; private $temp; /** * sets file. * * @param uploadedfile $file */ public function setfile(uploadedfile $file = null) { $this->file = $file; // check if have old image path if (isset($this->path)) { // store old name delete after update $this->temp = $this->path; $this->path = null; } else { $this->path = 'initial'; } } /** * @orm\prepersist() * @orm\preupdate() */ public function preupload() { if (null !== $this->getfile()) { // whatever want generate unique name $filename = sha1(uniqid(mt_rand(), true)); $this->path = $filename.'.'.$this->getfile()->guessextension(); } } /** * @orm\postpersist() * @orm\postupdate() */ public function upload() { if (null === $this->getfile()) { return; } // if there error when moving file, exception // automatically thrown move(). prevent // entity beingness persisted database on error $this->getfile()->move($this->getuploadrootdir(), $this->path); //$this->fixorientation($this->getabsolutepath()); //create polaroid $this->effectpolaroid($this->getabsolutepath(), 3); // check if have old image if (isset($this->temp)) { // delete old image unlink($this->getuploadrootdir().'/'.$this->temp); // clear temp image path $this->temp = null; } $this->file = null; } }
and controller:
public function uploadwebcamaction(request $request) { $foto = new foto(); $form = $this->createformbuilder($foto, array('csrf_protection' => false)) ->add('file', 'file') ->getform(); $form->handlerequest($request); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($foto); $em->flush(); homecoming $this->redirect($this->generateurl("foto_share", array('foto' => $foto->getid()))); } }
javascript php symfony2 blob
No comments:
Post a Comment