jquery uploadify如何取消已上傳成功文件
如何使用uploadify進(jìn)行文件上傳,各位都能夠在網(wǎng)上找到,但是需要注意版本號(hào).我這里僅僅說一下,在文件已經(jīng)成功上傳到服務(wù)器之后,如何取消文件的上傳.
我使用的是自動(dòng)上傳,即將'auto'屬性設(shè)置為true.
1.首先我們要設(shè)置cancelmg屬性,即設(shè)置文件上傳成功后,顯示在文件上的關(guān)閉圖片.這里需要修改對(duì)應(yīng)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的地址設(shè)置正確.這時(shí)可以看到上傳的文件后會(huì)顯示對(duì)應(yīng)的取消關(guān)閉圖片.當(dāng)然我們不修改源碼,將圖片放置在img文件夾下也可以.
2.當(dāng)我們使用自動(dòng)上傳,點(diǎn)擊文件對(duì)應(yīng)上的關(guān)閉,這時(shí)是不會(huì)觸發(fā)'onCancel'事件的,(onCancel事件是針對(duì)不自動(dòng)上傳時(shí)進(jìn)行觸發(fā)的)所以我們需要需要綁定對(duì)應(yīng)的事件到取消圖片上.
3.當(dāng)每個(gè)圖片上傳成功之后,都會(huì)觸發(fā)”onUploadSuccess”事件.所以我們將綁定操作寫在onUploadSuccess函數(shù)中.
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.首先調(diào)用ajax 傳遞文件名到后臺(tái),后臺(tái)刪除對(duì)應(yīng)的文件(這個(gè)我就不寫了)
//2.從后臺(tái)返回的為true,表明刪除成功;返回false,表明刪除失敗
var deletefileid = cancel.attr("deletefileid");
$("#uploadify").uploadify("cancel",deletefileid);//將上傳隊(duì)列中的文件刪除.
});
}
}
5.$("#uploadify").uploadify("cancel",deletefileid); 這會(huì)調(diào)用uploadify中的cancel方法,但是cancel方法中有一個(gè)問題,通過查看源碼,發(fā)現(xiàn)cancel方法并沒有將隊(duì)列中的文件刪除,只是在前臺(tái)刪除了對(duì)應(yīng)的div.這樣就會(huì)導(dǎo)致,假設(shè)當(dāng)我上傳文件A,已經(jīng)上傳成功,這時(shí)我點(diǎn)擊刪除圖片,取消文件A的上傳,這時(shí)前臺(tái)A文件消失,但是假如我再次上傳文件A,會(huì)提示我已經(jīng)上傳過文件A了,這顯然是有問題的.
其實(shí),uploadify的cancel方法就是針對(duì)還沒有上傳到服務(wù)器的文件,這時(shí)點(diǎn)擊取消,調(diào)用cancel方法,即cancel方法針對(duì)的是還沒有上傳到服務(wù)器的文件.
這時(shí)我們需要修改源碼將對(duì)應(yīng)需要?jiǎng)h除的文件在隊(duì)列中進(jìn)行刪除.
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;
/* 添加結(jié)束 */
$('#' + 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();
});
}
});
},
總結(jié)
以上是我針對(duì)如何取消已經(jīng)上傳成功的文件的方法.當(dāng)然如果不是自動(dòng)上傳,那么不用修改uploadify,直接刪除就好。
- JQuery上傳插件Uploadify使用詳解及錯(cuò)誤處理
- JQuery.uploadify 上傳文件插件的使用詳解 for ASP.NET
- php+jQuery.uploadify實(shí)現(xiàn)文件上傳教程
- Jquery Uploadify上傳帶進(jìn)度條的簡(jiǎn)單實(shí)例
- 詳解jquery uploadify 上傳文件
- Jquery上傳插件 uploadify v3.1使用說明
- Jquery Uploadify多文件上傳帶進(jìn)度條且傳遞自己的參數(shù)
- jQuery文件上傳插件Uploadify使用指南
- firefox瀏覽器用jquery.uploadify插件上傳時(shí)報(bào)HTTP 302錯(cuò)誤
- jquery uploadify和apache Fileupload實(shí)現(xiàn)異步上傳文件示例
相關(guān)文章
jQuery實(shí)現(xiàn)的縱向下拉菜單實(shí)例詳解【附demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的縱向下拉菜單,結(jié)合實(shí)例形式分析了jQuery動(dòng)態(tài)操作頁面元素實(shí)現(xiàn)縱向下拉菜單的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
jquery attr()設(shè)置和獲取屬性值實(shí)例教程
在JS中設(shè)置節(jié)點(diǎn)的屬性與屬性值用到setAttribute(),獲得節(jié)點(diǎn)的屬性與屬性值用到getAttribute(),而在jquery中,只需要用到attr()這個(gè)函數(shù)就可以了。attr是attribute(屬性)的縮寫。2016-09-09
jQuery實(shí)現(xiàn)頁面內(nèi)錨點(diǎn)平滑跳轉(zhuǎn)特效的方法總結(jié)
通過jQuery實(shí)現(xiàn)頁面內(nèi)錨點(diǎn)平滑跳轉(zhuǎn)的方法很多,可以通過插件hovertreescroll實(shí)現(xiàn),也可以簡(jiǎn)單的通過animate方法實(shí)現(xiàn),下面介紹這2種比較簡(jiǎn)單的方法。2015-05-05
JQuery調(diào)用WebServices的方法和4個(gè)實(shí)例
你是不是經(jīng)常作這種開發(fā),前端用JS寫邏輯,后端用aspx或者ashx作服務(wù)?你是不是經(jīng)常在請(qǐng)求aspx的時(shí)候在查詢字符串中拼接諸如a.aspx?method=getDepartmetn¶m1=1¶m2=2的字符串?2014-05-05
WordPress 照片lightbox效果的運(yùn)用幾點(diǎn)
應(yīng)該大家都知曉lightbox這類燈箱效果了,它一般更多地被運(yùn)用在網(wǎng)站照片的顯示上。當(dāng)然還有更推廣的應(yīng)用,如facebox這種更漂亮全面的效果。2009-06-06
jQuery UI Autocomplete 體驗(yàn)分享
jQuery UI Autocomplete是jQuery UI的自動(dòng)完成組件,是我用過的最強(qiáng)大、最靈活的Autocomplete,它支持本地的Array/JSON數(shù)組、通過ajax請(qǐng)求的Array/JSON數(shù)組、JSONP、以及Function(最靈活)等方式來獲取數(shù)據(jù)2012-02-02

