Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter AJAX File Upload is not working
    primarykey
    data
    text
    <p>I have written a mechanism to upload a file using AJAX technology (pure javascript) in CodeIgniter.</p> <p>Explanation:</p> <p><strong>1</strong>- I have written a script.js file which is responsible to handle AJAX/Javascript process of the upload.</p> <p><strong>2</strong>- I have written a controller in CodeIgniter which receives request from AJAX to upload the file.</p> <p><strong>3</strong>- I have written a simple HTML page</p> <p><strong>PROBLEM</strong>: When I hit the upload button, simply nothing happens! No error is shown. I think there is a problem with the transfer of data between javascript and php. Because I have used an almost similar code in regular php page and has been successful.</p> <p>Here are files:</p> <p>This is JAVASCRIPT</p> <pre><code>// JavaScript Document var doUpload = function(event){ // globally used variables in this function var progressBar = document.getElementById('progressBar'); event.preventDefault(); // prevents the default action of an element event.stopPropagation(); // get the file-input id var fileId = document.getElementById('file'); // this variable makes an object which accept key/value pairs which could be sent via ajax.send var formObj = new FormData(); // append currently selected file to the dataObject formObj.append('file', fileId.files[0]); // this is a variable to check in the php script (controller if the codeIgniter is used) formObj.append('error-check', true); formObj.append('finish-check', true); // let's make the ajax request object var ajaxReq = new XMLHttpRequest(); // PROGRESS OF THE FILE ///////////////////////////////////////////// // now trigger a function during the progress-process of the file-upload process ajaxReq.upload.addEventListener('progress', function(event){ console.log('this is a very good.'); // first let's get the amount of the file loaded. it is in decimals var percent = event.loaded / event.total; // get the name of the element that the progress-indicator is outputted there if(event.lengthComputable) // if a file is inserted and everything is just OK for uploading { if(progressBar.hasChildNodes()) // cleans the div container for a new progress to display { progressBar.removeChild(progressBar.firsChild); } progressBar.appendChild(document.createTextNode('The Progress So Far: '+percent*100+' %')); } // END OF PROGRESS OF THE FILE ///////////////////////////////////////////// // LOAD OF THE FILE ///////////////////////////////////////////// ajaxReq.upload.addEventListener('load', function(event){ progressBar.appendChild(document.createTextNode(" Completed!")); }); // END OF LOAD OF THE FILE ///////////////////////////////////////////// // ERROR OF THE FILE ///////////////////////////////////////////// ajaxReq.upload.addEventListener('error', function(event){ progressBar.removeChild(); progressBar.appendChild(document.createTextNode("Failed to Upload the File.")); }); // END OF THE ERROR OF THE FILE ///////////////////////////////////////////// // JSON // OPEN THE AJAX REQUEST ajaxReq.open('POST', 'upload/uploader'); // Set the header of the POST request ajaxReq.setRequestHeader('Cache-Control', 'no-cache'); // send the file. remember, we shoud pass a formData object as an argument to the ajaxRequest.send(); ajaxReq.send(formObj); }); } window.addEventListener('load', function(event) { // get the submit id of the form var submitButton = document.getElementById('submit'); submitButton.addEventListener('click', doUpload); }); </code></pre> <p>This is PHP Controller in CodeIgniter</p> <pre><code>&lt;?php class Upload extends CI_Controller { function index() { $this-&gt;load-&gt;view('pages/form'); } function uploader () { // define the required settings for the upload library to instanciate $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|doc|txt'; $config['max_size'] = 1024 * 8; $config['encrypt_name'] = TRUE; // load the upload library $this-&gt;load-&gt;library('upload', $config); if(!$this-&gt;upload-&gt;do_upload('file')) { $data['error'] = $this-&gt;upload-&gt;display_errors(); //$this-&gt;load-&gt;view('pages/form', $data); json_encode($data['error']); } else { $data['uploaded'] = $this-&gt;upload-&gt;data(); //$this-&gt;load-&gt;view('pages/form', $data); } } } </code></pre> <p>This is the HTML</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Upload Form With Ajax&lt;/title&gt; &lt;script src="&lt;?php echo base_url(); ?&gt;js/script.js" type='text/javascript'&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='error' style='color: red;height: 40px; width: 200px;'&gt; &lt;?php if(!empty($error)){echo $error; } ?&gt; &lt;/div&gt; &lt;form id='form' name='form' enctype="multipart/form-data" &gt; &lt;input type='file' name='file' id='file'/&gt; &lt;input type='submit' name='submit' id='submit' value='Upload File' /&gt; &lt;/form&gt; &lt;div style='height: 200px; width: 300px; color: red; padding: 10px; background-color: #CCC;' id='progressBar'&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload