/** * 通用附件上传和初始化方法 * */ $(function() { /*新增页面图片上传代码初始化*/ /** 1)在新增业务添加addPageUpload容器 * 2)上传图片时页面没有对应业务的数据,先给附件生产一个临时的uuid * 3)保存业务单据后,需要将附件的临时id,更新为业务单据的id */ if($("#addPageUpload")&&$("#addPageUpload").length>0){ var $receiptsInfoId=$("<input/>").attr({"type":"hidden","id":"receiptsInfoId","name":"receiptsInfoId"}).val(Math.uuidFast()); var $file_upload=$("<input/>").attr({"id":"file_upload","name":"file_upload","type":"file","multiple":"true"}); var $thead=$("<thead><tr><th>附件名称</th><th>上传时间</th><th>操作</th></tr></thead>"); var $tbody=$("<tbody id='fileList'></tbody>"); var $table=$("<table/>").attr({"id":"tableId","class":"table table-bordered"}).append($thead).append($tbody); $("#addPageUpload").empty().append($receiptsInfoId).append($file_upload).append($table); } /*编辑页面图片上传代码初始化*/ /** * 1)编辑页面有要<input type='hidden' id='item_id' name="id" value='........'> * 2)定义id=editPageUpload的容器 * 3)该方法会自动加载对应单据的附件列表(可删除可添加) */ if($("#editPageUpload")&&$("#editPageUpload").length>0){ var $receiptsInfoId=$("<input/>").attr({"type":"hidden","id":"receiptsInfoId","name":"receiptsInfoId"}).val($("#item_id").val()); var $file_upload=$("<input/>").attr({"id":"file_upload","name":"file_upload","type":"file","multiple":"true"}); var $thead=$("<thead><tr><th>附件名称</th><th>上传时间</th><th>操作</th></tr></thead>"); var $tbody=$("<tbody id='fileList'></tbody>"); var $table=$("<table/>").attr({"id":"tableId","class":"table table-bordered"}).append($thead).append($tbody); //获取附件列表,可编辑 $.ajax({ url: basePath+'jsp/fileInfo/findfiles.action', data:'receiptsInfoId='+$("#item_id").val(), async:false, dataType:'json', success: function(data){ if(data!='none'){ $.each(data,function(index,fileinfo){ var $tdFileName=$("<td/>").text(fileinfo.fileName); var $tdCreated=$("<td/>").text(fileinfo.remark); var $ADownload=$("<a href=\"javascript:void(0);\" onclick=\"downloadFile('"+fileinfo.url+"', '"+fileinfo.fileName+"', '"+fileinfo.name+"')\" >下载</a>"); var $ADel=" <a onclick=\"file_del('"+fileinfo.id+"')\" href=\"javascript:void(0);\">删除</a>"; var $divOpt=$("<div/>").attr({"class":"tools-operations"}).append($ADownload).append($ADel); var $tdOpt=$("<td/>").append($divOpt); var $tr=$("<tr/>").attr({"id":fileinfo.id}).append($tdFileName).append($tdCreated).append($tdOpt); $tbody.append($tr); }); } } }); $("#editPageUpload").empty().append($receiptsInfoId).append($file_upload).append($table); } /** * 1)查看页面有要<input type='hidden' id='item_id' name="id" value='........'> * 2)定义id=viewPageUpload的容器 * 3)该方法会自动加载对应单据的附件列表(只读) */ if($("#viewPageUpload")&&$("#viewPageUpload").length>0){ var $thead=$("<thead><tr><th>附件名称</th><th>上传时间</th><th>操作</th></tr></thead>"); var $tbody=$("<tbody id='fileList'></tbody>"); var $table=$("<table/>").attr({"id":"tableId","class":"table table-bordered"}).append($thead).append($tbody); //获取附件列表,可编辑 $.ajax({ url: basePath+'jsp/fileInfo/findfiles.action', data:'receiptsInfoId='+$("#item_id").val(), async:false, dataType:'json', success: function(data){ if(data!='none'){ $.each(data,function(index,fileinfo){ var $tdFileName=$("<td/>").text(fileinfo.fileName); var $tdCreated=$("<td/>").text(fileinfo.remark); var $ADownload=$("<a href=\"javascript:void(0);\" onclick=\"downloadFile('"+fileinfo.url+"', '"+fileinfo.fileName+"', '"+fileinfo.name+"')\" >下载</a>"); var $divOpt=$("<div/>").attr({"class":"tools-operations"}).append($ADownload); var $tdOpt=$("<td/>").append($divOpt); var $tr=$("<tr/>").attr({"id":fileinfo.id}).append($tdFileName).append($tdCreated).append($tdOpt); $tbody.append($tr); }); }else{ var $tr=$("<tr class='no_data'><td colspan='3'>没有上传过附件</td></tr>"); $tbody.append($tr); } } }); $("#viewPageUpload").empty().append($table); } /** *基于uploadify 附件上传方法 */ if($("#file_upload")&&$("#file_upload").length>0){ $('#file_upload').uploadify({ 'buttonText' : '附件上传', 'buttonImage' : basePath+'assets/uploadify/browse-btn.png', 'swf' : basePath+'assets/uploadify/uploadify.swf', 'uploader' : basePath+'servlet/fileInfoUpload.gho', 'formData' : {'receiptsInfoId':''}, 'method' : 'post', 'height': 30, 'width': 120, 'onUploadStart' : function(file) { var name=$('#receiptsInfoId').val(); $("#file_upload").uploadify("settings", "formData", {'receiptsInfoId':name}); }, 'onUploadSuccess' : function(file, data, response) { var jsonData=jQuery.parseJSON(data); $("#fileList").append("<tr id='"+jsonData.id+"'><td>"+jsonData.fileName+"</td><td>"+jsonData.created+"</td><td><div class='tools-operations'><a href=\"#\" onclick=\"downloadFile('"+jsonData.url+"', '"+jsonData.fileName+"', '"+jsonData.name+"')\" >下载</a> <a onclick=\"file_del('"+jsonData.id+"')\" href=\"javascript:void(0);\">删除</a></div></td></tr>"); $("#"+file.id).remove(); } }); } }); /**附件通用下载方法*/ function downloadFile(url,fileName,name){ //创建动态表单,将这些数据发送到后台 var $form=$("<form/>").attr({"id":"fileForm","name":"fileForm","method":"post"}); var timestamp = (new Date()).valueOf(); $form.attr("action",basePath+"servlet/fileInfoDownload.gho?timestamp=" + timestamp); //创建文本域传递要查询的内容 var $path=$("<input/>").attr({"type":"hidden","name":"path","id":"path"}).val(url); var $name=$("<input/>").attr({"type":"hidden","name":"name","id":"name"}).val(name); var $filename=$("<input/>").attr({"type":"hidden","name":"filename","id":"filename"}).val(fileName); //将文本域添加到表单上 $form.append($path).append($name).append($filename); //提交表单 $("body").append($form);//必须追加到body上否则在ie下不能提交 document.getElementById("fileForm").submit(); } /**通用附件删除方法*/ function file_del(_id){ showMsgConfirmWithIcon("操作提示","您确认要删除该附件吗?","附件一经删除不可恢复!",function(opt){ if(opt){ $.ajax({ type:'post', url:basePath+'jsp/fileInfo/del.action', data:{id:_id}, dataType:'json', success:function(msg){ if(msg.flag=='ok'){ //window.location.reload(true); $("#"+_id).remove(); }else{ showMsgAlert("提示信息",msg.msg); } } }); }else{ } }); }
文章评论