【发布时间】:2014-12-15 03:24:24
【问题描述】:
我可能以一个完全错误的想法来处理这个问题,所以让我先解释一下我的情况。
我试图使用 jQuery-File-Upload 实现的是允许用户选择他们选择上传的文件。他们将被允许选择多个文件。现在不管是什么类型,但他们主要是上传图片。选定的文件将出现在一个列表中,每个文件的文件名右侧都有一个“删除”按钮。如下图所示。
如何实现允许用户从等待上传的文件列表中删除他们选择的文件?
一旦用户对他们选择的文件感到满意,他们将单击“开始上传”按钮,该按钮将开始上传列表中包含的文件并触发上图所示的进度条。
我自己尝试了一些故障排除,包括注销单击按钮时发生的事件,但无法将其从选择上传的文件列表中删除。我知道有一个drop 回调,但我不确定这是否是我应该使用的。
下面是我的代码。
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: '../php/',
add: function (e, data) {
//$.each(data.files, function(index, file) {
data.context = $('<li class=\"list-group-item\">')
//.html(file.name+"<button type=\"button\" id=\"drop\" class=\"btn btn-danger btn-xs pull-right\"><span class=\"glyphicon glyphicon-remove\"></span></button>")
// see https://stackoverflow.com/questions/26234279/blueimp-jquery-upload-multiple-files-doesnt-work for the reason for the line below
.html(data.files[0].name+"<button type=\"button\" id=\"drop\" class=\"btn btn-danger btn-xs pull-right\"><span class=\"glyphicon glyphicon-trash\"></span></button>")
.appendTo(".list-group");
/*$('.btn-danger').on('click', function() {
console.log('Drop '+file.name+' \n');
});*/
//});
$('.btn-danger').on('click', function(e, data) {
//console.log("Removing all objects...\n");
//data.context.remove(data.files[0].name);
//data.context.remove(data.files[0]);
e.preventDefault();
/*var filename = $(this).parent().text();
console.log("Filename: "+filename);
data.context.remove(data.files.filename);
console.log("Clicked the drop button");*/
try { // here I try thinking that I can call the drop() callback but I'm still trying to wrap my head around how this all works.
drop(e, data);
} catch(error) {
console.log("The error was: "+error);
}
});
},
submit: function (e, data) {
$('#start-upload').on('click', function() {
//$('#start-upload').addClass('#disabledInput');
console.log("This is the start upload button!");
});
},
done: function (e, data) {
/*$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo('.files').find('#panel-body');
});*/
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css(
'width',
progress + '%'
);
},
drop: function (e, data) {
$.each(data.files, function (index, file) {
//$('.btn-danger').on('click', function() {
console.log('Dropped file: '+ file.name +'\n');
//});
});
}
}).on('.fileuploadsubmit', 'click', function(e, data) {
console.log("Submit button pressed.");
//data.formData = data.context.find(':input').seralizeArray();
});
});
最后,所有这些都应该放在我的$(document).ready 中吗?
我确实意识到这可能是 this post 的重新发布,但这是两个问题合而为一,我认为我将其细分为更小的问题。
【问题讨论】:
标签: javascript jquery file-upload jquery-file-upload blueimp