[eluser]SPeed_FANat1c[/eluser]
I have a link, I press it and it uploads the file. When the file is uploaded, the user may want to change the file, so press the link again. The old file shoud be removed from server.
OnComplete callback changes the variables
change and
old_file. I tested, at onSubmit callback those variables are like I want. But in there
Code:
data: {change: change, old_file: old_file}
it looks that they remain the same as on initialization. So the server still 'thinks' that user uploads a new file. Have no idea how to make them change. Only thiing I think I can do - in on submit callback use jquery post to, to call the seperate server function to remove old file.
So why the post data does not change?
Code:
var change = 'false'; //on upload complete we will change this to true
var old_file;
function upload_for_new()
{
/*
* nuotraukos ikelimas su valums ajax upload
*/
var button = $('#button1'), interval;
var base_url = CI.base_url;
new AjaxUpload(button, {
action: CI.base_url + "admin_/darbuotojai/do_upload_for_new",
name: 'userfile',
data: {change: change, old_file: old_file}, //server will know if it has to remove the old file
onSubmit : function(file, ext){
// change button text, when user selects file
button.text('Siunčiama');
//console.log(change); //there it show ok data
// If you want to allow uploading only 1 file at time,
// you can disable upload button
this.disable();
// Uploding -> Uploading. -> Uploading...
interval = window.setInterval(function(){
var text = button.text();
if (text.length < 13){
button.text(text + '.');
} else {
button.text('Siunčiama');
}
}, 200);
},
//response - file name
onComplete: function(file, response){
button.text('Pakeisti nuotrauką');
$('.del_picture').show();
window.clearInterval(interval);
// enable upload button
this.enable();
//console.log(response);
if(response != 'not_admin')
{
//if response ends with :err, it means upload error
var err = response.split(':').pop().toLowerCase();
if(err != 'err')
{
change = 'true';
old_file = response;
$('div.foto').html('<img src="'+ CI.base_url + 'uploads/darbuotojai/' + response + '"/>');
$('div.foto').show();
}
else //upload error
{
var error = response.split(':',1);
jAlert(error[0]);
}
}
else [removed].replace(base_url + "admin");
//console.log(change);
}
});
}