I am using Jasny Bootstrap to display file preview when it is selected.
<div class="fileinput fileinput-new" data-provides="fileinput">
<div>
<span class="btn btn-primary btn-file"><span class="fileinput-new"><span class="fa fa-camera"></span> Image 3</span>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
<input type="file" name="file" class="ephoto-upload" accept="image/jpeg"></span>
</div>
</div>
I would like to start submission of this image via ajax as soon as image is selected. I am using following code.
<script>
$('.ephoto-upload').change(function(){
if($(this).val()!='') {
var formData = new FormData();
formData.append('file', $(this)[0].files[0]);
$.ajax({
url: '/path/to/upload',
type: 'POST',
data: formData,
async: false,
success: function (r) {
if(r.success) {
//success work
}
},
cache: false,
contentType: false,
processData: false
});
}
});
</script>
Upload works fine but the preview of image is displayed after ajax upload is completed.So for sometime page get freezes and after that preview is displayed. But I want to display the preview as soon as image gets selected and after displaying the selected image, Ajax get execute.