读书人

jquery ajax上传资料

发布时间: 2012-06-27 14:20:09 作者: rapoo

jquery ajax上传文件

?

1.页面引入

?

<script type="text/javascript" src=/js/ajaxupload.js' ></script>

?

2.页面加入

?

<input type="file" name="attachment" style="display: none;"><a href="javascript:void(0);" id="fileUploadProxy">增加附件</a>

?

3.给指定Id注册事件

?

 jQuery(function(){var fileUploadProxy = jQuery('#fileUploadProxy');var themeImagesPath = jQuery('input[name="themeImagesPath"]').val();var taskName = jQuery('input[name="taskNameHid"]').val();var uploadAttachmentUrl = jQuery('input[name="uploadAttachmentUrl"]').val();var downloadAttachmentUrl = jQuery('input[name="downloadAttachmentUrlHid"]').val();if (fileUploadProxy.length > 0) {new AjaxUpload(fileUploadProxy, {action : uploadAttachmentUrl,//上传的URLname : 'attachment',//file 框的nameonSubmit : function(file, ext) {//fileUploadProxy.disable();}, onComplete : function(file, response) {var attachmentId = response;var fileExt = file.substring(file.indexOf('.') + 1);var attachmentTr = '<tr>';attachmentTr += '<td border="0" src="'+ themeImagesPath+'/document_library/'+fileExt+'.png" />';attachmentTr += '&nbsp';attachmentTr += '<a href="'+ downloadAttachmentUrl + '&attachmentId='+ attachmentId + '">'+ file +'</a>';attachmentTr += '</td>';attachmentTr += '<td>';attachmentTr += '<input type="hidden" value="'+ attachmentId +'"/>';attachmentTr += '<img src="/images/close.png">';attachmentTr += '</td>';attachmentTr += '</tr>';jQuery('.task_attachment_list').append(attachmentTr);//fileUploadProxy.enable();jQuery('.removeAttachmentImg').bind('click', function(){var attachmentUrl = jQuery('input[name="deleteAattachmentUrlHid"]').val();var attachmentId = jQuery(this).prev().val();jQuery(this).attr('id', 'removeImg');jQuery.get(attachmentUrl,{attachmentId: attachmentId}, function(data){if (data == 'success') {jQuery('#removeImg').parent().parent().remove();} else {alert('delete faild !');}});});}});}});  
?

读书人网 >Ajax

热点推荐