php - Yii: Unable to upload file using Ajax -


after reading accepted answer on this post, trying implement same not able figure out problem. when select file, nothing available in $_files on server side. doing wrong? using apache 2.2.22, yii 1.12

my view file

<form enctype="multipart/form-data" action='/webapp/index.php/emu/default/uploadfile' method="post">     <input type="hidden" name="max_file_size" value="30000" />     <input id="files" type="file" > </form> <script> document.getelementbyid('files').addeventlistener('change', function(e) {     var file = this.files[0];     var xhr = new xmlhttprequest();     xhr.file = file; // not necessary if create scopes     xhr.addeventlistener('progress', function(e) {         var done = e.position || e.loaded, total = e.totalsize || e.total;         console.log('xhr progress: ' + (math.floor(done/total*1000)/10) + '%');     }, false);     if ( xhr.upload ) {         xhr.upload.onprogress = function(e) {             var done = e.position || e.loaded, total = e.totalsize || e.total;             console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (math.floor(done/total*1000)/10) + '%');         };     }     xhr.onreadystatechange = function(e) {         if ( 4 == this.readystate ) {             console.log(['xhr upload complete', e]);         }     };     var url='/webapp/index.php/emu/default/uploadfile';     xhr.open('post', url, true);     xhr.send(file); }, false); </script> 

controller action:

public function actionuploadfile(){     yii::log(cjson::encode($_files['files'])); } 

yii::log outputs following:

2014/12/16 19:59:29 [error] [php] undefined index: files  

to upload file transparently via ajax you'll have use formdata object

var file = this.files[0]; var xhr = new xmlhttprequest(); var data = new formdata(); data.append('files', file); ... xhr.send(data); 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -