php - CodeIgniter Upload File Ajax not working -
no matter can't seem file upload via ajax codeigniter method. throws , never sees uploaded file.
html
<div> <h1>upload translation</h1> </div> <form method="post" class="myform" enctype="multipart/form-data"> <!-- add span , pther stuff here--> <input type="file" id="foto1" name="userfile" /> <input type="button" value="submit" onclick="submitfile();" /> </form> <script> function submitfile(){ var formurl = "/system_administration/ajax_upload_translation"; var formdata = new formdata($('.myform')[0]); $.ajax({ url: formurl, type: 'post', data: formdata, mimetype: "multipart/form-data", contenttype: false, cache: false, processdata: false, success: function(data, textsatus, jqxhr){ //now here response returned php in json fomat can parse using json.parse(data) }, error: function(jqxhr, textstatus, errorthrown){ //handle here error returned } }); } </script>
payload
request method:post status code:200 ok ------webkitformboundaryvkn2bt8zdxxmkj7y content-disposition: form-data; name="userfile"; filename="clippy-windows-8-10.jpg" content-type: image/jpeg function ajax_upload_translation() { if (!isset($_files['userfile']['error']) || is_array($_files['userfile']['error'])) { throw new runtimeexception('invalid parameters.'); } }
try code..
$('#upload').on('click', function() { var file_data = $('#foto1').prop('files')[0]; var form_data = new formdata(); form_data.append('file', file_data); $.ajax({ url: 'your_upload_php_file', // point server-side php script datatype: 'text', // expect php script, if cache: false, contenttype: false, processdata: false, data: form_data, type: 'post', success: function(php_script_response){ alert(php_script_response); // display response php script, if } }); });