Commit 426373c6 authored by Marc Duez's avatar Marc Duez

database.js : split upload_file in two ajax call

- the first send file info (the server return a ticket id, secure by https)
- the second upload the file asynchronously (using ticket for safety)
parent e5ffb265
function Database(id, db_address) {
this.db_address = db_address;
this.id = id;
this.upload = 0;
this.upload = {};
}
Database.prototype = {
......@@ -64,6 +65,7 @@ Database.prototype = {
//TODO : implémenter un flash message
if (res.message) console.log("database log : "+res.message)
return res
}
catch(err)
{
......@@ -137,48 +139,36 @@ Database.prototype = {
//submit formulaire avec fichier
if ( document.getElementById('upload_form') ){
//$('#upload_form').on('submit', self.upload_form ); // doesn't work :/
var upload_n=this.upload
//rename form
var data = $('#upload_form').serialize()
$('#upload_form').ajaxForm({
type : "POST",
cache: false,
crossDomain: true,
xhrFields: {withCredentials: true},
url : $(this).attr('action'),
data : $(this).serialize(),
beforeSubmit: function() {
self.call("patient/index")
//crée un div qui contiendra la progression de l'upload du fichier
self.upload++;
var div = document.createElement('div');
var spanName=document.createElement('span');
spanName.innerHTML = $("#upload_file").val().split('/').pop().split('\\').pop();
var spanPercent=document.createElement('span');
spanPercent.id = "upload_percent_"+upload_n;
spanPercent.innerHTML = '0%';
success : function(result) {
var js = self.display_result(result)
var id = js.file_id
var fileSelect = document.getElementById('upload_file');
var files = fileSelect.files;
var data = new FormData();
div.appendChild(spanName);
div.appendChild(spanPercent);
for (var i = 0; i < files.length; i++) {
var file = files[i];
data.append('file', file, file.name);
}
data.append('id', id);
var div_parent=document.getElementById("upload_list");
div_parent.appendChild(div);
},
//mise a jour progressive du % d'upload
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
$('#upload_percent_'+upload_n).html(percentVal);
},
success : function(result) {
self.display_result(result)
self.upload_file(id, data)
},
error: function (request, status, error) {
if(status==="timeout") {
popupMsg("timeout");
} else {
popupMsg(request.responseText);
popupMsg(request + " " + status + " " + error);
}
}
});
......@@ -187,6 +177,38 @@ Database.prototype = {
},
upload_file: function (id, data){
var self = this;
$.ajax({
type: "POST",
crossDomain: true,
url: self.db_address + "file/upload",
processData: false,
contentType: false,
data: data,
beforeSend: function(){
self.upload[id] = 0
},
success: function (result) {
self.upload[id] = 1
self.upload_display()
self.display_result(result)
},
error: function (request, status, error) {
delete self.upload[id];
self.upload_display();
if (status === "timeout") {
popupMsg("timeout");
} else {
popupMsg(request.responseText);
}
}
});
return data
},
/* appel une fonction du serveur
* idem que call() mais la réponse n'est pas une page html a afficher
* mais simplement une confirmation que la requete a été entendu
......@@ -331,6 +353,19 @@ Database.prototype = {
.style.display = "block";
document.getElementById("db_msg")
.innerHTML = msg;
this.upload_display();
},
upload_display: function(){
for (var key in this.upload){
if (this.upload[key]==1){
$("#sequence_file_"+key).html("uploaded !")
delete this.upload[key];
}else{
$("#sequence_file_"+key).html("<div class='loading_seq'></div>")
}
}
},
//efface et ferme la fenetre de dialogue avec le serveur
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment