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').val();var taskName = jQuery('input').val();var uploadAttachmentUrl = jQuery('input').val();var downloadAttachmentUrl = jQuery('input').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 class="yj_t_2">';attachmentTr += taskName;attachmentTr += '</td>';attachmentTr += '<td class="fj_mc">';attachmentTr += '<img align="left" 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 class="removeAttachmentImg" 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').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 !');}});});}});}});
页:
[1]