這篇文章將為大家詳細講解有關jquery uploadify怎么取消已上傳成功文件,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
如何使用uploadify進行文件上傳,各位都能夠在網上找到,但是需要注意版本號.我這里僅僅說一下,在文件已經成功上傳到服務器之后,如何取消文件的上傳.
我使用的是自動上傳,即將'auto'屬性設置為true.
1.首先我們要設置cancelmg屬性,即設置文件上傳成功后,顯示在文件上的關閉圖片.這里需要修改對應CSS中的代碼
.uploadify-queue-item .cancel a {
background: url('../img/uploadify-cancel.png') 0 0 no-repeat;
float: right;
height: 16px;
text-indent: -9999px;
width: 16px;
}將這里url中的uploadify-cancel.png的地址設置正確.這時可以看到上傳的文件后會顯示對應的取消關閉圖片.當然我們不修改源碼,將圖片放置在img文件夾下也可以.
2.當我們使用自動上傳,點擊文件對應上的關閉,這時是不會觸發'onCancel'事件的,(onCancel事件是針對不自動上傳時進行觸發的)所以我們需要需要綁定對應的事件到取消圖片上.
3.當每個圖片上傳成功之后,都會觸發”onUploadSuccess”事件.所以我們將綁定操作寫在onUploadSuccess函數中.
4.代碼如下:
onUploadSuccess:function(file, data, response) {
var cancel=$('#fileQueue .uploadify-queue-item[id="' + file.Id + '"]').find(".cancel a");
if (cancel) {
cancel.attr("deletefileid",file.id);
cancel.click(function () {
//我的處理邏輯
//1.首先調用ajax 傳遞文件名到后臺,后臺刪除對應的文件(這個我就不寫了)
//2.從后臺返回的為true,表明刪除成功;返回false,表明刪除失敗
var deletefileid = cancel.attr("deletefileid");
$("#uploadify").uploadify("cancel",deletefileid);//將上傳隊列中的文件刪除.
});
}
}5.$("#uploadify").uploadify("cancel",deletefileid); 這會調用uploadify中的cancel方法,但是cancel方法中有一個問題,通過查看源碼,發現cancel方法并沒有將隊列中的文件刪除,只是在前臺刪除了對應的div.這樣就會導致,假設當我上傳文件A,已經上傳成功,這時我點擊刪除圖片,取消文件A的上傳,這時前臺A文件消失,但是假如我再次上傳文件A,會提示我已經上傳過文件A了,這顯然是有問題的.
其實,uploadify的cancel方法就是針對還沒有上傳到服務器的文件,這時點擊取消,調用cancel方法,即cancel方法針對的是還沒有上傳到服務器的文件.
這時我們需要修改源碼將對應需要刪除的文件在隊列中進行刪除.
cancel : function(fileID, supressEvent) {
var args = arguments;
this.each(function() {
// Create a reference to the jQuery DOM object
var $this = $(this),
swfuploadify = $this.data('uploadify'),
settings = swfuploadify.settings,
delay = -1;
if (args[0]) {
// Clear the queue
if (args[0] == '*') {
var queueItemCount = swfuploadify.queueData.queueLength;
$('#' + settings.queueID).find('.uploadify-queue-item').each(function() {
delay++;
if (args[1] === true) {
swfuploadify.cancelUpload($(this).attr('id'), false);
} else {
swfuploadify.cancelUpload($(this).attr('id'));
}
$(this).find('.data').removeClass('data').html(' - Cancelled');
$(this).find('.uploadify-progress-bar').remove();
$(this).delay(1000 + 100 * delay).fadeOut(500, function() {
$(this).remove();
});
});
swfuploadify.queueData.queueSize = 0;
swfuploadify.queueData.queueLength = 0;
// Trigger the onClearQueue event
if (settings.onClearQueue) settings.onClearQueue.call($this, queueItemCount);
} else {
for (var n = 0; n < args.length; n++) {
swfuploadify.cancelUpload(args[n]);
/* 添加代碼 */
delete swfuploadify.queueData.files[args[n]];
swfuploadify.queueData.queueLength = swfuploadify.queueData.queueLength - 1;
/* 添加結束 */
$('#' + args[n]).find('.data').removeClass('data').html(' - Cancelled');
$('#' + args[n]).find('.uploadify-progress-bar').remove();
$('#' + args[n]).delay(1000 + 100 * n).fadeOut(500, function() {
$(this).remove();
});
}
}
} else {
var item = $('#' + settings.queueID).find('.uploadify-queue-item').get(0);
$item = $(item);
swfuploadify.cancelUpload($item.attr('id'));
$item.find('.data').removeClass('data').html(' - Cancelled');
$item.find('.uploadify-progress-bar').remove();
$item.delay(1000).fadeOut(500, function() {
$(this).remove();
});
}
});
},關于“jquery uploadify怎么取消已上傳成功文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。