I am using the jQuery form plugin to upload a file and show the progress bar.
the JS code :
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
var options = {
target: '.result',
url: 'slider_insert_now.php',
beforeSend: function() {
var img_file = $.trim($(".img_file").val());
if(img_file==$.trim('')){
$(".result").html('<span class="error">No file selected</span>');
return false;
}else{
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
return true;
}
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
success: function() {
var percentVal = '100%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
status.html(xhr.responseText);
}
};
$('#form_upload').ajaxForm(options);
And the HTML:
<div class="result"></div>
<form id="form_upload" action="javascript:void(0)" method="POST" enctype="multipart/form-data">
<div class="progress">
<div class="bar"></div>
<div class="percent">0%</div>
</div>
<div id="status"></div>
<input type="file" value="" name="img_file" class="img_file" />
<br></br>
<input type="submit" value="Upload the Image" />
</form>
in the beforeSend
function, I used return false
to prevent the form from submitting itself. But it does not work . How can I achieve that?