I can't upload my files like described in several tutorials I even followed multiple guides here on stackoverflow, everytime I get the ajax error.
My html form is in codeigniter and it is like the following:
<?php echo form_open_multipart($this->uri->uri_string(), 'class="form-horizontal"'); ?>
<fieldset>
<div class="control-group <?php echo form_error('gid') ? 'error' : ''; ?>">
<?php echo form_label('Image Gallery', 'athletes_gid', array('class' => 'control-label')); ?>
<div class='controls' >
<input id='athletes_gid' type='file' name='athletes_gid[]' multiple value="<?php echo set_value('athletes_gid[]', isset($athletes['gid']) ? $athletes['gid'] : time()); ?>" /><br/><br/>
<div id="addimg" class="btn">Add images</div><br/><br/>
<input type="submit" name="save" class="btn btn-primary" value="<?php echo lang('athletes_action_edit'); ?>" />
</fieldset>
<?php echo form_close(); ?>
Basicly just normal file input field. And my jquery goes like:
//ajax img upload
// Variable to store your files
var files;
// Add events
$('#athletes_gid').on('change', prepareUpload);
// Grab the files and set them to our variable
function prepareUpload(event)
{
files = event.target.files;
}
//So now you have a FormData object, ready to be sent along with the XMLHttpRequest.
$( "#addimg" ).click(function() {
var data = new FormData();
$.each(files, function(key, value)
{
data.append(key, value);
});
$.ajax({
url: "/public/index.php/admin/content/athletes/multiUpload",
data: {data: JSON.stringify(data), ci_csrf_token: $("input[name=ci_csrf_token]").val()},
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
});
If I click on that button "Add images" after I added some picture files to upload, nothing... I read somewhere firefox doesn't like contentType: false
but I am not sure...
Well, Javascript can't access filesystem for obvious security reasons.
What i do in this cases, is creating a hidden iframe and then i submit my form targeting this frame. With Javascript i'm able to read the iframe html, and then i know the result of the upload.
Another solution, is a hidden swf ( flash ) that is accessed by javascript and you do all the upload task using this swf.