I have a form that takes a json file and POSTs to a server side process. It's a lengthy process and I want to message it's progress back to the user in real time with multiple messages.
What are my options?
This process can take 10-15 minutes or higher. I am not looking for the answer "AJAX". there is more to it than that.
this is what I got for a form:
<form method="POST" accept-charset="UTF-8" class="smart-form" id="import-course" enctype="multipart/form-data">
<fieldset>
<div class="row">
<section class="col col-md-12">
<label class="input input-file">
<input class="button" name="import" type="file">
</label>
</section>
</div>
</fieldset>
<footer>
<button class="btn btn-primary" id="submit-import-file" type="button">Save</button>
<a onclick="history.back();" class="btn btn-default">Cancel</a>
</footer>
</form>
Here is my ajax:
$(document).ready(function(){
$("#submit-import-file").on('click',function(){
console.log('click');
$('#import-course').hide();
var formData = new FormData($('#import-course')[0]);
$.ajax({
url: '{{URL::route("courses.import")}}', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
//Ajax events
success: function(data){
console.log(data);
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
});
function progressHandlingFunction(e){
console.log(e);
}
and here is my server side pay attention to the comments.
$errors = 0;
$file = Input::file('import');
$fileName = $file->getClientOriginalName();
$destinationPath = app_path()."/storage/files/";
$file->move($destinationPath, $fileName);
$course = json_decode(File::get($destinationPath.$fileName));
if(!File::isDirectory($destinationPath.$course->code)){
File::makeDirectory($destinationPath.$course->code,0777,true,true);
//message back directory created
}
foreach($course->file as $file){
if(FileManger->processfile($file)){
//message back $file->name imported
}else{
//message back error importing $file->name
}
}
return "import complete";
So now.. How do i get the comment areas to be messaged back to the user while this processes. not after.