当前位置:   article > 正文

FTP ftp.printWorkingDirectory() 乱码问题以及FTP浏览效果实例_开发环境 ftpclient.printworkingdirectory()

开发环境 ftpclient.printworkingdirectory()

乱码问题

获取项目当前路径 ftp.printWorkingDirectory() 乱码,因为已经是转码的了,再转回来即可

new String(ftp.printWorkingDirectory().getBytes("iso8859-1"), "UTF-8")

下载文件返回为false,文件大小为0:

ftp.retrieveFile(ftp.printWorkingDirectory()+"/"

+new String((fileName).getBytes(LOCAL_CHARSET),"ISO-8859-1"), is);

思路:

1、ftp.printWorkingDirectory() 不要放在new String里,否则再次转码,获取不到地址

2、文件路径连接符用"/",不要用File.separator


我的实例:

效果

ftp文件浏览

后台代码

  1. package com.hljtx.eip.contorller;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.zip.ZipOutputStream;
  13. import javax.annotation.Resource;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.apache.commons.net.ftp.FTPClient;
  18. import org.apache.commons.net.ftp.FTPFile;
  19. import org.apache.commons.net.ftp.FTPReply;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.ResponseBody;
  23. import com.hljtx.eip.service.impl.IntelligentReportSecondServiceImpl;
  24. import com.hljtx.eip.util.FTPUtil;
  25. import com.hljtx.eip.util.Prop;
  26. import com.hljtx.eip.util.PropUtils;
  27. @Controller
  28. @RequestMapping("/FTPTool")
  29. public class FTPToolController {
  30. private static String LOCAL_CHARSET = "GBK";
  31. @Resource
  32. IntelligentReportSecondServiceImpl reportSecondServiceImpl;
  33. @RequestMapping("/showFileDirectory.do")
  34. public String showFileDirectory(HttpServletRequest request,HttpServletResponse response) {
  35. try{
  36. String ftpHost = request.getParameter("ftpHost");
  37. String ftpPortStr= request.getParameter("ftpPort");
  38. int ftpPort =0;
  39. if(StringUtils.isNotBlank(ftpPortStr)){
  40. ftpPort = Integer.parseInt(request.getParameter("ftpPort"));
  41. }
  42. String ftpUserName = request.getParameter("ftpUserName");
  43. String ftpPassword = request.getParameter("ftpPassword");
  44. String url = request.getParameter("url");
  45. String rootUrl = request.getParameter("root");
  46. if(StringUtils.isBlank(url)){
  47. url = rootUrl;
  48. }
  49. if(StringUtils.endsWith(rootUrl,"/")){
  50. rootUrl=rootUrl.substring(0,rootUrl.lastIndexOf("/"));
  51. }
  52. request.setAttribute("ftpHost", ftpHost);
  53. request.setAttribute("ftpPort", ftpPort);
  54. request.setAttribute("ftpUserName", ftpUserName);
  55. request.setAttribute("ftpPassword", ftpPassword);
  56. request.setAttribute("root", rootUrl);
  57. FTPUtil ftpUtil = new FTPUtil();
  58. FTPClient ftp = ftpUtil.getFTPClient(ftpHost, ftpPort, ftpUserName, ftpPassword);
  59. if (FTPReply.isPositiveCompletion(
  60. ftp.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
  61. LOCAL_CHARSET= "UTF-8";
  62. }
  63. ftp.setControlEncoding(LOCAL_CHARSET);
  64. //设置FTP连接模式
  65. ftp.enterLocalPassiveMode();
  66. if(StringUtils.isNotBlank(url)){
  67. ftp.changeWorkingDirectory(new String(url.getBytes(LOCAL_CHARSET),"ISO-8859-1"));
  68. if(ftp.printWorkingDirectory().equals("/")){
  69. request.setAttribute("msg", "文件打开发生错误,请检查文件权限");
  70. return "jsp/ftptool/ftplist";
  71. }
  72. }
  73. FTPFile[] fileList=ftp.listFiles();
  74. //返回文件列表
  75. request.setAttribute("fileList", fileList);
  76. System.out.println(ftp.printWorkingDirectory());
  77. System.out.println(new String(ftp.printWorkingDirectory().getBytes("iso8859-1"), "UTF-8"));
  78. System.out.println(new String(ftp.printWorkingDirectory().getBytes(LOCAL_CHARSET),"ISO-8859-1"));
  79. System.out.println(ftp.getCharsetName());
  80. System.out.println(rootUrl);
  81. if(StringUtils.isNotBlank(rootUrl)){
  82. url = url.replace(rootUrl, "");
  83. }
  84. request.setAttribute("url", url);
  85. }catch(Exception e){
  86. e.printStackTrace();
  87. }
  88. return "jsp/ftptool/ftplist";
  89. }
  90. @RequestMapping("/downFtpFile.do")
  91. @ResponseBody
  92. public Map<String,Object> downFtpFile(HttpServletRequest request,HttpServletResponse response) {
  93. Map<String,Object> map= new HashMap<String, Object>();
  94. String AccessApacheUrl ="";
  95. //获取项目路径
  96. Prop prop = PropUtils.use("config.properties");
  97. String fileUploadPath = prop.get("fileUploadPath");
  98. String fileAccessApacheUrl = prop.get("fileAccessApacheUrl");
  99. //文件夹名称
  100. String tempFileName="ftpTempFile";
  101. //文件夹路径
  102. String tempFileUrl= fileUploadPath + File.separator + tempFileName ;
  103. try {
  104. String ftpHost = request.getParameter("ftpHost");
  105. String ftpPortStr= request.getParameter("ftpPort");
  106. int ftpPort =0;
  107. if(StringUtils.isNotBlank(ftpPortStr)){
  108. ftpPort = Integer.parseInt(request.getParameter("ftpPort"));
  109. }
  110. String ftpUserName = request.getParameter("ftpUserName");
  111. String ftpPassword = request.getParameter("ftpPassword");
  112. String url = request.getParameter("url");
  113. String fileName = request.getParameter("fileName");
  114. String folder = request.getParameter("folder");
  115. FTPUtil ftpUtil = new FTPUtil();
  116. FTPClient ftp = ftpUtil.getFTPClient(ftpHost, ftpPort, ftpUserName, ftpPassword);
  117. if (FTPReply.isPositiveCompletion(
  118. ftp.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
  119. LOCAL_CHARSET= "UTF-8";
  120. }
  121. ftp.setControlEncoding(LOCAL_CHARSET);
  122. //设置FTP连接模式
  123. ftp.enterLocalPassiveMode();
  124. //打不开文件夹
  125. if(StringUtils.isNotBlank(url)){
  126. ftp.changeWorkingDirectory(new String(url.getBytes(LOCAL_CHARSET),"ISO-8859-1"));
  127. System.out.println(ftp.printWorkingDirectory());
  128. if(ftp.printWorkingDirectory().equals("/")){
  129. return map;
  130. }
  131. }
  132. //时间戳
  133. String time=String.valueOf(new Date().getTime());
  134. tempFileUrl = tempFileUrl+File.separator+time;
  135. File tempFile = new File(tempFileUrl);
  136. tempFile.mkdirs();
  137. //下载文件
  138. if(StringUtils.isNotBlank(fileName)){
  139. String temfileName = tempFileUrl+File.separator+fileName;
  140. File localFile = new File(temfileName);
  141. localFile.deleteOnExit();
  142. OutputStream is = new FileOutputStream(localFile);
  143. ftp.setFileType(ftp.BINARY_FILE_TYPE);
  144. ftp.retrieveFile(ftp.printWorkingDirectory()+"/"+new String((fileName).getBytes(LOCAL_CHARSET),"ISO-8859-1"), is);
  145. is.close();
  146. AccessApacheUrl = fileAccessApacheUrl + tempFileName+File.separator+time +File.separator+fileName;
  147. }else{
  148. //下载文件夹
  149. FTPFile[] fileList=ftp.listFiles();
  150. if(fileList.length>0){
  151. String fileDi= tempFileUrl+File.separator +folder;
  152. File fileDIF = new File(fileDi);
  153. fileDIF.mkdirs();
  154. for(FTPFile ftpFile:fileList){
  155. if(ftpFile.getType() == 1){
  156. downLoadFile(ftp, ftpFile, fileDi+File.separator+ftpFile.getName());
  157. }else{
  158. downLoadFile(ftp, ftpFile, fileDi);
  159. }
  160. }
  161. String zipUrl = tempFileUrl+".zip";
  162. // 实例化 FileOutputStream 对象
  163. FileOutputStream fileOutputStream = new FileOutputStream(zipUrl);
  164. // 创建 ZipOutputStream
  165. ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
  166. reportSecondServiceImpl.makeZip(zipOutputStream,tempFile,"");
  167. //关闭各种流
  168. zipOutputStream.closeEntry();
  169. zipOutputStream.close();
  170. fileOutputStream.close();
  171. AccessApacheUrl = fileAccessApacheUrl+ tempFileName+File.separator+time+".zip";
  172. //压缩完成删除压缩前的文件
  173. File srcFiles = new File(tempFileUrl);
  174. reportSecondServiceImpl.deleteAllFile(srcFiles);
  175. }
  176. }
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. AccessApacheUrl="";
  180. }
  181. map.put("AccessApacheUrl", AccessApacheUrl);
  182. return map;
  183. }
  184. public void downLoadFile(FTPClient ftp,FTPFile ftpFile,String loaclFileUrl) {
  185. try{
  186. File localFile = new File(loaclFileUrl);
  187. localFile.mkdirs();
  188. //文件夹类型
  189. if(ftpFile.getType() == 1){
  190. if(ftp.changeWorkingDirectory(ftpFile.getName())){
  191. System.out.println(ftp.printWorkingDirectory());
  192. //下载文件夹
  193. FTPFile[] fileList=ftp.listFiles();
  194. for(FTPFile nextftpFile:fileList){
  195. System.out.println(ftpFile.getType() );
  196. if(nextftpFile.getType() == 1){
  197. downLoadFile(ftp, nextftpFile, loaclFileUrl+File.separator+nextftpFile.getName());
  198. }else{
  199. downLoadFile(ftp, nextftpFile, loaclFileUrl);
  200. }
  201. }
  202. }
  203. }else{
  204. localFile.deleteOnExit();
  205. OutputStream is = new FileOutputStream(localFile+File.separator+ftpFile.getName());
  206. ftp.setFileType(ftp.BINARY_FILE_TYPE);
  207. ftp.retrieveFile(ftp.printWorkingDirectory()+"/"+new String(ftpFile.getName().getBytes(LOCAL_CHARSET),"ISO-8859-1"), is);
  208. is.close();
  209. }
  210. }catch(Exception e){
  211. e.printStackTrace();
  212. }
  213. }
  214. @RequestMapping("/downFile.do")
  215. public void downFile(HttpServletRequest request,HttpServletResponse response) {
  216. try {
  217. String url = request.getParameter("url");
  218. if(StringUtils.isNotBlank(url)){
  219. //获取项目路径
  220. Prop prop = PropUtils.use("config.properties");
  221. String fileUploadPath = prop.get("fileUploadPath");
  222. String fileAccessApacheUrl = prop.get("fileAccessApacheUrl");
  223. String temfileName = url.replace(fileAccessApacheUrl, fileUploadPath);
  224. File file = new File(temfileName);
  225. String fileName=temfileName.substring(temfileName.lastIndexOf("\\")+1, temfileName.length());
  226. // 以流的形式下载文件。
  227. InputStream fis = new BufferedInputStream(new FileInputStream(temfileName));
  228. byte[] buffer = new byte[fis.available()];
  229. fis.read(buffer);
  230. fis.close();
  231. // 清空response
  232. response.reset();
  233. // 设置response的Header
  234. response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1"));
  235. //设置文件打下
  236. response.addHeader("Content-Length", "" + file.length());
  237. OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  238. response.setContentType("application/octet-stream");
  239. toClient.write(buffer);
  240. toClient.flush();
  241. toClient.close();
  242. }
  243. } catch (Exception e) {
  244. e.printStackTrace();
  245. }
  246. }
  247. }

 前端页面代码

ftplist.jsp

  1. <%@page pageEncoding="utf-8" import="java.util.*,com.hljtx.eip.util.ConsumerStyleChange,com.hljtx.eip.util.SsoUtils"%>
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
  4. <%@ taglib prefix="fun" uri="http://java.sun.com/jsp/jstl/functions"%>
  5. <%
  6. String path = request.getContextPath();
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  8. %>
  9. <!DOCTYPE html>
  10. <html lang="zh-CN" style="background: #fff;">
  11. <head>
  12. <head>
  13. <base href="<%=basePath%>">
  14. <title>ftp文件浏览</title>
  15. <meta charset="utf-8">
  16. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  17. <meta name="viewport" content="width=device-width, initial-scale=1">
  18. <link rel="stylesheet" href="<%=path %>/css/folder.css" />
  19. <link rel="stylesheet" href="<%=path %>/css/bootstrap.min.css" />
  20. <script src="<%=path %>/js/jquery.min.js"></script>
  21. <script src="<%=path %>/js/database.js"></script>
  22. <script src="<%=path %>/js/dblist.js"></script>
  23. <script src="<%=path %>/js/jquery.contextmenu.r2.js"></script>
  24. <script src="<%=path %>/js/folder.js"></script>
  25. <script src="<%=path%>/js/newlayer/layer.js"></script>
  26. <script type="text/javascript">
  27. $(function(){
  28. //初始化
  29. var id = "1";
  30. init(id, 0);
  31. //textarea_bind();
  32. });
  33. </script>
  34. <script type="text/javascript">
  35. $(function(){
  36. //双击功能
  37. $("#divall li.folder").dblclick(function() {
  38. openFile($(this));
  39. });
  40. var url = $("[name='url']").val()
  41. if("${msg}" !=""){
  42. alert("${msg}");
  43. if( url !="" && url !="/"){
  44. openRootFile();
  45. }
  46. }
  47. })
  48. //进入文件夹
  49. function openFile(obj){
  50. var folder = $(obj).children("input.changename").val();
  51. var root = $("[name='root']").val();
  52. var oldUrl = $("[name='url']").val();
  53. if(oldUrl.trim()=="/" || oldUrl.trim()==""){
  54. $("[name='url']").val(root+"/"+folder);
  55. }else{
  56. $("[name='url']").val(root+"/"+oldUrl+"/"+folder);
  57. }
  58. $("#ftpForm").submit()
  59. }
  60. /**
  61. *打开根目录
  62. */
  63. function openRootFile(){
  64. var root = $("[name='root']").val();
  65. $("[name='url']").val(root);
  66. $("#ftpForm").submit()
  67. }
  68. //点击导航事件
  69. function openNavigationFile(obj){
  70. var root = $("[name='root']").val();
  71. var newUrl=""
  72. $(obj).prevAll(".childFolder").each(function(){
  73. newUrl +="/"+$(this).text().trim();
  74. })
  75. newUrl +="/" + $(obj).text().trim();
  76. $("[name='url']").val(root+newUrl);
  77. $("#ftpForm").submit()
  78. }
  79. var layerIndex;
  80. //下载文件
  81. function downFTPFile(param){
  82. layer.load({
  83. shade: [0.1,'#fff'] //0.1透明度的白色背景
  84. });
  85. var root = $("[name='root']").val();
  86. var oldUrl = $("[name='url']").val();
  87. var newUrl="";
  88. var filename="";
  89. var data={};
  90. if(param.is_directory ==1){
  91. if(oldUrl.trim()=="/" || oldUrl.trim()==""){
  92. newUrl=root+"/"+param.folder_name
  93. }else{
  94. newUrl=root+"/"+oldUrl+"/"+param.folder_name;
  95. }
  96. //文件夹类型
  97. data.url = newUrl;
  98. data.folder =param.folder_name;
  99. }else{
  100. //文件类型
  101. if(oldUrl.trim()=="/" || oldUrl.trim()==""){
  102. newUrl=root
  103. }else{
  104. if(oldUrl.startsWith("/")){
  105. newUrl=root+oldUrl;
  106. }else{
  107. newUrl=root+"/"+oldUrl;
  108. }
  109. }
  110. data.url = newUrl;
  111. filename=param.folder_name;
  112. }
  113. data.fileName = filename;
  114. data.ftpHost=$("[name='ftpHost']").val();
  115. data.ftpPort=$("[name='ftpPort']").val();
  116. data.ftpUserName=$("[name='ftpUserName']").val();
  117. data.ftpPassword=$("[name='ftpPassword']").val();
  118. $.ajax({
  119. type:'post',
  120. url:'<%=path %>/FTPTool/downFtpFile.do',
  121. data:data,
  122. success:function(d){
  123. layer.closeAll('loading');
  124. if(d.AccessApacheUrl && d.AccessApacheUrl !=""){
  125. //询问框
  126. layerIndex = layer.confirm('文件地址:'+d.AccessApacheUrl+',是否下载?', {
  127. closeBtn: 0,
  128. btn: ['是','否'] //按钮
  129. }, function(){
  130. window.open(d.AccessApacheUrl, '_blank');
  131. <%-- if(param.is_directory ==1){
  132. window.open(d.AccessApacheUrl, '_self');
  133. }else{
  134. window.open("<%=path %>/FTPTool/downFile.do?url="+d.AccessApacheUrl,"下载",)
  135. } --%>
  136. }, function(){
  137. layer.close(layerIndex);
  138. });
  139. }else{
  140. alert("没有可下载文件")
  141. }
  142. }
  143. })
  144. }
  145. </script>
  146. </head>
  147. <body>
  148. <!-- 导航 -->
  149. <div class="navigation-bar">
  150. <div class="forward-backward">
  151. <button type="button" class="backward flipy-y btn-xs k-btn btn" title="后退" onclick="history.go(-1)"></button>
  152. </div>
  153. <button type="button" class="home btn-xs k-btn btn" title="返回根目录" onclick="openRootFile()"></button>
  154. <div class="folder-navigation" id="folder-navigation">
  155. <a class="foldername" data-id="1" onclick="openRootFile()">根目录</a>
  156. <c:forEach items="${fun:split(url,'/') }" var="fileName" varStatus="i">
  157. <c:if test="${!empty fileName }">
  158. <img class="triangle" src="<%=path %>/images/triangle.png"><a class="foldername childFolder" data-id="2" onclick="openNavigationFile(this)">${fileName }</a>
  159. </c:if>
  160. </c:forEach>
  161. </div>
  162. </div>
  163. <form action="<%=path %>/FTPTool/showFileDirectory.do" method="post" id="ftpForm" name="ftpForm">
  164. <input type="hidden" value="${root}" name="root">
  165. <input type="hidden" value="${url}" name="url">
  166. <input type="hidden" value="${ftpHost}" name="ftpHost">
  167. <input type="hidden" value="${ftpPort}" name="ftpPort">
  168. <input type="hidden" value="${ftpUserName}" name="ftpUserName">
  169. <input type="hidden" value="${ftpPassword}" name="ftpPassword">
  170. </form>
  171. <div class="alldom" id="all_folder">
  172. <ul id="divall">
  173. <c:forEach items="${fileList }" var="file">
  174. <c:set var="fileTypeName" value="${fun:substringAfter(file.name,'.') }" />
  175. <c:set var="fileType" value="${fun:toLowerCase(fileTypeName) }" />
  176. <li
  177. <c:choose>
  178. <c:when test="${file.type eq 1}">
  179. class="folder"
  180. </c:when>
  181. <c:when test="${fileType eq 'docx' || fileType eq 'doc'}">
  182. class="file docx"
  183. </c:when>
  184. <c:when test="${fileType eq 'xls' || fileType eq 'xlsx'}">
  185. class="file xlsx"
  186. </c:when>
  187. <c:when test="${fileType eq 'pdf'}">
  188. class="file pdf"
  189. </c:when>
  190. <c:when test="${fileType eq 'png'}">
  191. class="file png"
  192. </c:when>
  193. <c:when test="${fileType eq 'jpg'}">
  194. class="file jpg"
  195. </c:when>
  196. <c:when test="${fileType eq 'mp3'}">
  197. class="file mp3"
  198. </c:when>
  199. <c:when test="${fileType eq 'mp4'}">
  200. class="file mp4"
  201. </c:when>
  202. <c:when test="${fileType eq 'rar' || fileType eq 'zip'}">
  203. class="file rar"
  204. </c:when>
  205. <c:when test="${fileType eq 'txt'}">
  206. class="file txt"
  207. </c:when>
  208. <c:otherwise>
  209. class="file other-filetype"
  210. </c:otherwise>
  211. </c:choose>
  212. title="${file.name }" index="0"><input type="text" class="changename" value="${file.name }" data-id="2" disabled="disabled" data-last-value="${file.name }"></li>
  213. </c:forEach>
  214. </ul>
  215. </div>
  216. <div style="clear: both;"></div>
  217. <!-- 在文件夹上右键菜单 -->
  218. <div class="contextMenu" id="myMenu2">
  219. <ul>
  220. <li id="open" >打开</li>
  221. <li id="download">下载</li>
  222. <!-- <li id="copy">复制</li>
  223. <li id="cut">剪切</li>
  224. <li id="paste">黏贴</li>
  225. <li id="rename">重命名</li>
  226. <li id="delete">删除</li>-->
  227. </ul>
  228. </div>
  229. <!-- 在文件上面右键菜单 -->
  230. <div class="contextMenu" id="myMenu3">
  231. <ul>
  232. <li id="download">下载</li>
  233. <!-- <li id="copy">复制</li>
  234. <li id="cut">剪切</li>
  235. <li id="rename">重命名</li>
  236. <li id="delete">删除</li>-->
  237. </ul>
  238. </div>
  239. </body>
  240. </html>

folder.css

  1. @CHARSET "UTF-8";
  2. * {
  3. margin:0;
  4. padding:0;
  5. }
  6. body,html {
  7. width:100%;
  8. height:100%;
  9. }
  10. .alldom {
  11. }
  12. .alldom ul {
  13. padding:0;
  14. list-style:none;
  15. }
  16. .folder {
  17. background:url(../images/213125.png) center top no-repeat;
  18. }
  19. .doc,.docx {
  20. background:url(../images/DOC_File_Extension.png) center top no-repeat;
  21. }
  22. .pdf {
  23. background:url(../images/pdf.png) center top no-repeat;
  24. }
  25. .xls,.xlsx {
  26. background:url(../images/XLS_File_Extension.png) center top no-repeat;
  27. }
  28. .other-filetype {
  29. background:url(../images/qita.png) center top no-repeat;
  30. }
  31. .png {
  32. background:url(../images/png.png) center top no-repeat;
  33. }
  34. .jpg {
  35. background:url(../images/jpg.png) center top no-repeat;
  36. }
  37. .txt {
  38. background:url(../images/file.png) center top no-repeat;
  39. }
  40. .mp3 {
  41. background:url(../images/mp3.png) center top no-repeat;
  42. }
  43. .mp4 {
  44. background:url(../images/mp4.png) center top no-repeat;
  45. }
  46. .rar {
  47. background:url(../images/rar.png) center top no-repeat;
  48. }
  49. .alldom ul li {
  50. overflow:hidden;
  51. width:100px;
  52. height:115px;
  53. float:left;
  54. margin: 0px 25px;
  55. border:1px solid #fff;
  56. position:relative;
  57. transition:all 0.2s ease-in 0.1s;
  58. white-space:nowrap;
  59. text-overflow:ellipsis
  60. }
  61. .alldom ul li.focus {
  62. }
  63. .alldom ul li:hover {
  64. background-color:#ddd;
  65. cursor: pointer;
  66. }
  67. input.changename {
  68. position:absolute;
  69. border-radius:0px;
  70. left:-25px;
  71. bottom:10px;
  72. overflow:hidden;
  73. width:98px;
  74. height:20px;
  75. color:#595c5f;
  76. line-height:20px;
  77. text-align:center;
  78. font-size:12px;
  79. z-index:0;
  80. border:0;
  81. border-radius:8px;
  82. transition:all 0.2s ease-in 0.1s;
  83. margin-left:25px;
  84. background:none;
  85. }
  86. input.changename:hover {
  87. background:none;
  88. color:#F33;
  89. }
  90. input.changename:focus {
  91. background-color:#fff;
  92. }
  93. .info-bar {
  94. height:65px;
  95. }
  96. .info-icon {
  97. width:60px;
  98. height:90%;
  99. display:inline-block;
  100. background-size:cover;
  101. }
  102. .info-detail {
  103. display:inline-block;
  104. height:90%;
  105. width:90%;
  106. margin-left:10px;
  107. }
  108. .detail-form .detail-field {
  109. float:left;
  110. width:300px;
  111. }
  112. .detail-field2 {
  113. font-size:large;
  114. margin-left:10px;
  115. }
  116. .detail-var {
  117. margin-right:10px;
  118. }
  119. .navigation-bar .home {
  120. width:24px;
  121. height:24px;
  122. background-image:url(../images/blue_home.png);
  123. background-repeat:no-repeat;
  124. background-size:cover;
  125. margin:0 3px;
  126. position:relative;
  127. display:inline-block;
  128. }
  129. .navigation-bar .home:hover {
  130. background-color:#ccc;
  131. }
  132. .navigation-bar .gotopre {
  133. width:24px;
  134. height:24px;
  135. background-image:url(../images/return.png);
  136. background-repeat:no-repeat;
  137. background-size:cover;
  138. margin:0 3px;
  139. position:relative;
  140. display:inline-block;
  141. background-color:ghostwhite;
  142. }
  143. .navigation-bar .gotopre:hover {
  144. background-color:#0ce2ea;
  145. }
  146. .folder-navigation .foldername {
  147. cursor:pointer;
  148. text-decoration:none;
  149. }
  150. .folder-navigation .triangle {
  151. width:8px;
  152. margin:0 5px 3px 5px;
  153. }
  154. .navigation-bar .folder-navigation {
  155. height:20px;
  156. position:relative;
  157. display:inline-block;
  158. margin-left:10px;
  159. }
  160. .navigation-bar .forward-backward {
  161. width:30px;
  162. display:inline-block;
  163. }
  164. .navigation-bar .forward-backward .backward {
  165. background-image:url(../images/forward.png);
  166. background-repeat:no-repeat;
  167. background-size:contain;
  168. margin:5px 3px;
  169. border:0;
  170. width:24px;
  171. height:24px;
  172. }
  173. .navigation-bar .forward-backward .backward:hover,.navigation-bar .forward-backward .backward:focus {
  174. background-color:#ccc;
  175. }
  176. .navigation-bar .forward-backward .forward {
  177. background-image:url(../images/forward.png);
  178. background-repeat:no-repeat;
  179. background-size:contain;
  180. margin:0 3px;
  181. border:0;
  182. width:24px;
  183. height:24px;
  184. background-color:#1da71d;
  185. }
  186. .navigation-bar .forward-backward .forward:hover,.navigation-bar .forward-backward .forward:focus {
  187. }
  188. .flipy-x {
  189. -moz-transform:scaleY(-1);
  190. -webkit-transform:scaleY(-1);
  191. -o-transform:scaleY(-1);
  192. transform:scaleY(-1);
  193. filter:FlipV;
  194. }
  195. .flipy-y {
  196. filter:FlipH;
  197. }
  198. .folder-navigation a{ color:#5d5d5d}
  199. .navigation-bar{
  200. border-bottom: solid 1px #eee;
  201. margin-bottom: 15px;
  202. }

folder.js(大部分没用上,只有绑定菜单用到了)

  1. //定义双向链表存放浏览历史
  2. var operation_history = new DbList({
  3. "parent_id": "-1",
  4. "is_active": false
  5. }),
  6. timeOutFn = null,
  7. is_ctrl_down = false,
  8. is_shift_down = false,
  9. focus_index = -1;
  10. /*mode:1->复制 2->剪切
  11. * id:被复制或剪切的文件或文件夹的id
  12. * parent_id:被复制文件的上级目录id
  13. * can_paste:false->不能黏贴 true->能黏贴
  14. * */
  15. oprate_param = {
  16. "mode": "",
  17. "id": "",
  18. "parent_id": "",
  19. "can_paste": false
  20. };
  21. function init(parent_id, mode) {
  22. //初始化导航栏
  23. //navigation(parent_id);
  24. //将当前页面插入历史记录链表里面
  25. /*mode
  26. *0:初始化进入页面
  27. *1:新建文件夹
  28. *2:删除文件夹或文件
  29. *3:刷新当前文件夹
  30. *4:将当前目录下的文件夹和文件排序
  31. *5:上传文件
  32. *6:打开文件夹
  33. *7:点击后退按钮
  34. *8:点击前进按钮
  35. *9:点击主页
  36. *10:返回上一层目录
  37. *11:点击地址栏
  38. *
  39. */
  40. if (mode == 0) {
  41. operation_history.insertLast({
  42. "parent_id": parent_id,
  43. "is_active": true
  44. });
  45. } else if (mode == 1) {
  46. //新建文件夹不插入历史记录
  47. } else if (mode == 2) {
  48. //删除文件夹或文件不会改变历史记录
  49. } else if (mode == 3) {
  50. //刷新当前文件夹不插入历史记录
  51. } else if (mode == 4) {
  52. //排序不插入历史记录
  53. } else if (mode == 5) {
  54. //上传文件现在会重新进入页面,这个之后改
  55. } else if (mode == 6) {
  56. //打开文件夹会删除父目录(也就是当前激活的目录)往后的所有记录,再插入新的子目录记录
  57. var currNode = find_active_node();
  58. operation_history.removeAfter(currNode);
  59. currNode.element.is_active = false;
  60. operation_history.insertLast({
  61. "parent_id": parent_id,
  62. "is_active": true
  63. });
  64. } else if (mode == 7) {
  65. var currNode = find_active_node();
  66. currNode.element.is_active = false;
  67. currNode.previous.element.is_active = true;
  68. } else if (mode == 8) {
  69. var currNode = find_active_node();
  70. currNode.element.is_active = false;
  71. currNode.next.element.is_active = true;
  72. } else if (mode == 9) {
  73. //返回主页会删除父目录(也就是当前激活的目录)往后的所有记录,再插入新的子目录(主页)记录
  74. var currNode = find_active_node();
  75. operation_history.removeAfter(currNode);
  76. currNode.element.is_active = false;
  77. operation_history.insertLast({
  78. "parent_id": parent_id,
  79. "is_active": true
  80. });
  81. } else if (mode == 10) {
  82. //返回上级目录会删除父目录(也就是当前激活的目录)往后的所有记录,再插入新的子目录(上级目录)记录
  83. var currNode = find_active_node();
  84. operation_history.removeAfter(currNode);
  85. currNode.element.is_active = false;
  86. operation_history.insertLast({
  87. "parent_id": parent_id,
  88. "is_active": true
  89. });
  90. } else if (mode == 11) {
  91. //点击地址栏仅仅往历史记录里面加一条记录,如果链表里面最后一条记录
  92. //与将要添加的相同则不添加记录而是把最后一条记录设为激活状态
  93. var currNode = find_active_node(),
  94. lastNode = operation_history.findLast();
  95. currNode.element.is_active = false;
  96. if (lastNode.element.parent_id == parent_id) {
  97. lastNode.element.is_active = true;
  98. } else {
  99. operation_history.insertLast({
  100. "parent_id": parent_id,
  101. "is_active": true
  102. });
  103. }
  104. }
  105. //载入文件夹
  106. //load();
  107. //载入信息栏 第一个参数-> 1:展示文件夹的记录数 2:展示选中的文件信息 3:展示选中的文件夹数目
  108. // 第二个参数-> 展示的文件的id
  109. info(1, 0);
  110. //绑定右键菜单
  111. contextMenu();
  112. //绑定左键事件
  113. leftClick();
  114. //绑定获取焦点事件
  115. //focus();
  116. //绑定失去焦点事件
  117. //blur();
  118. //定义文件名更改事件
  119. // change();
  120. //定义双击事件
  121. dbclick();
  122. //定义键盘按键按下事件
  123. //keydown();
  124. //定义键盘按键弹起事件
  125. //keyup();
  126. //将元素绑定拖拽方法--还没开发完
  127. //drag();
  128. }
  129. //在历史记录中找到当前的节点
  130. function find_active_node() {
  131. var currNode = operation_history.getHead();
  132. while (currNode.element.is_active != true) {
  133. currNode = currNode.next;
  134. }
  135. return currNode;
  136. }
  137. function contextMenu() {
  138. contextMenu_folder();
  139. contextMenu_blank();
  140. contextMenu_file();
  141. }
  142. function contextMenu_folder() {
  143. $("#divall li.folder").contextMenu('myMenu2', {
  144. bindings: {
  145. 'open': function(t) {
  146. openFile(t);
  147. },
  148. 'rename': function(t) {
  149. //重命名
  150. var folder = $(t).children("input.changename"),
  151. folder_name = folder.val(),
  152. id = folder.attr("data-id"),
  153. is_directory = $(t).hasClass("folder") ? 1 : 0;
  154. doc_type = $(t).hasClass("folder") ? "" : folder.attr("data-filetype"),
  155. parent_id = $("#navigation").val(),
  156. params = {
  157. "folder_name": folder_name,
  158. "id": id,
  159. "is_directory": is_directory,
  160. "doc_type": doc_type,
  161. "parent_id": parent_id,
  162. "description": ""
  163. };
  164. K.form.setparams($("#M8610F001"), params);
  165. K.popup($("#M8610P001"));
  166. },
  167. 'delete': function(t) {
  168. //删除文件夹
  169. var id = $(t).children("input.changename").attr("data-id"),
  170. is_directory = $(t).hasClass("folder") ? 1 : 0,
  171. params = {
  172. "id": id,
  173. "is_directory": is_directory
  174. };
  175. dele(params);
  176. },
  177. 'download': function(t) {
  178. //将文件夹打包下载
  179. var folder = $(t).children("input.changename"),
  180. id = folder.attr("data-id"),
  181. is_directory = $(t).hasClass("folder") ? 1 : 0,
  182. folder_name = folder.val(),
  183. parent_id = $("#navigation").val(),
  184. params = {
  185. "id": id,
  186. "is_directory": is_directory,
  187. "folder_name": folder_name,
  188. "parent_id": parent_id
  189. };
  190. downFTPFile(params);
  191. // Tools.alert("下载成功!");
  192. }/*,
  193. 'copy': function(t) {
  194. //复制
  195. var focus_id = [];
  196. $("#divall").find("li").each(function(i) {
  197. if ($(this).hasClass("focus")) {
  198. focus_id.push($(this).children("input.changename").attr("data-id"));
  199. }
  200. });
  201. oprate_param.mode = 1;
  202. oprate_param.id = focus_id;
  203. oprate_param.parent_id = $("#navigation").val();
  204. oprate_param.can_paste = true;
  205. alert("copy了:" + focus_id);
  206. },
  207. 'cut': function(t) {
  208. //剪切
  209. var focus_id = [];
  210. $("#divall").find("li").each(function(i) {
  211. if ($(this).hasClass("focus")) {
  212. focus_id.push($(this).children("input.changename").attr("data-id"));
  213. }
  214. });
  215. oprate_param.mode = 2;
  216. oprate_param.id = focus_id;
  217. oprate_param.parent_id = $("#navigation").val();
  218. oprate_param.can_paste = true;
  219. alert("cut了:" + focus_id);
  220. },
  221. 'paste': function(t) {
  222. var folder = $(t).children("input.changename"),
  223. id = folder.attr("data-id");
  224. if (oprate_param.can_paste != true) {
  225. alert("剪切板中无内容!");
  226. } else {
  227. if (oprate_param.parent_id == id) {
  228. alert("文件已存在!");
  229. } else {
  230. //黏贴
  231. }
  232. }
  233. }*/
  234. },
  235. onContextMenu: function(e) {
  236. var i_index = $(e.target).attr("index"),
  237. all_focus_index = [];
  238. $("#divall").find("li").each(function(i) {
  239. if ($(this).hasClass("focus")) {
  240. all_focus_index.push($(this).attr("index"));
  241. }
  242. });
  243. if ($.inArray(i_index, all_focus_index) == -1) {
  244. $("#divall").find("li").each(function(i) {
  245. $(this).removeClass("focus");
  246. });
  247. $(e.target).addClass("focus");
  248. }
  249. return true;
  250. }
  251. });
  252. }
  253. function contextMenu_file() {
  254. $("#divall li.file").contextMenu('myMenu3', {
  255. bindings: {
  256. /*'rename': function(t) {
  257. //重命名
  258. var folder = $(t).children("input.changename"),
  259. folder_name = folder.val(),
  260. id = folder.attr("data-id"),
  261. is_directory = $(t).hasClass("folder") ? 1 : 0,
  262. doc_type = $(t).hasClass("folder") ? "" : folder.attr("data-filetype"),
  263. parent_id = $("#navigation").val(),
  264. params = {
  265. "folder_name": folder_name,
  266. "id": id,
  267. "is_directory": is_directory,
  268. "doc_type": doc_type,
  269. "parent_id": parent_id,
  270. "description": ""
  271. };
  272. K.form.setparams($("#M8610F001"), params);
  273. K.popup($("#M8610P001"));
  274. },*/
  275. /*'copy': function(t) {
  276. //复制
  277. var focus_id = [];
  278. $("#divall").find("li").each(function(i) {
  279. if ($(this).hasClass("focus")) {
  280. focus_id.push($(this).children("input.changename").attr("data-id"));
  281. }
  282. });
  283. oprate_param.mode = 1;
  284. oprate_param.id = focus_id;
  285. oprate_param.parent_id = $("#navigation").val();
  286. oprate_param.can_paste = true;
  287. alert("copy了:" + focus_id);
  288. },
  289. 'cut': function(t) {
  290. //剪切
  291. var focus_id = [];
  292. $("#divall").find("li").each(function(i) {
  293. if ($(this).hasClass("focus")) {
  294. focus_id.push($(this).children("input.changename").attr("data-id"));
  295. }
  296. });
  297. oprate_param.mode = 2;
  298. oprate_param.id = focus_id;
  299. oprate_param.parent_id = $("#navigation").val();
  300. oprate_param.can_paste = true;
  301. alert("cut了:" + focus_id);
  302. },*/
  303. 'delete': function(t) {
  304. //删除单个文件
  305. var id = $(t).children("input.changename").attr("data-id"),
  306. is_directory = $(t).hasClass("folder") ? 1 : 0,
  307. params = {
  308. "id": id,
  309. "is_directory": is_directory
  310. };
  311. dele(params);
  312. },
  313. 'download': function(t) {
  314. //下载单个文件
  315. var folder = $(t).children("input.changename"),
  316. id = folder.attr("data-id"),
  317. is_directory = $(t).hasClass("folder") ? 1 : 0,
  318. folder_name = folder.val() //+ "." + folder.attr("data-filetype"),
  319. parent_id = $("#navigation").val(),
  320. params = {
  321. "id": id,
  322. "is_directory": is_directory,
  323. "folder_name": folder_name,
  324. "parent_id": parent_id
  325. };
  326. downFTPFile(params);
  327. // Tools.alert("下载成功!");
  328. }
  329. },
  330. onContextMenu: function(e) {
  331. var i_index = $(e.target).attr("index"),
  332. all_focus_index = [];
  333. $("#divall").find("li").each(function(i) {
  334. if ($(this).hasClass("focus")) {
  335. all_focus_index.push($(this).attr("index"));
  336. }
  337. });
  338. if ($.inArray(i_index, all_focus_index) == -1) {
  339. $("#divall").find("li").each(function(i) {
  340. $(this).removeClass("focus");
  341. });
  342. $(e.target).addClass("focus");
  343. }
  344. return true;
  345. }
  346. });
  347. }
  348. function contextMenu_blank() {
  349. $("#all_folder").contextMenu('myMenu1', {
  350. bindings: {
  351. 'newfolder': function(t) {
  352. //获取新文件夹的名称
  353. var folder_names = [],
  354. newfolder_name = "";
  355. $("#all_folder").find("ul").eq(0).find("li.folder").each(function(index) {
  356. folder_names.push($(this).children("input.changename").val());
  357. });
  358. for (var i = 0; i < 100; i++) {
  359. if (i == 0) {
  360. newfolder_name = "新文件夹";
  361. } else {
  362. newfolder_name = "新文件夹[" + i + "]";
  363. }
  364. if ($.inArray(newfolder_name, folder_names) == -1) {
  365. break;
  366. };
  367. }
  368. //调用新增文件夹代码
  369. var params = {
  370. "id": $("#navigation").val(),
  371. "discription": "",
  372. "folder_name": newfolder_name
  373. };
  374. var flag = add_folder(params);
  375. if (flag) {
  376. init($("#navigation").val(), 1);
  377. }
  378. },
  379. /*'paste': function(t) {
  380. //黏贴
  381. var parent_id = $("#navigation").val();
  382. if (oprate_param.can_paste != true) {
  383. Tools.alert("无黏贴内容");
  384. } else {
  385. if (oprate_param.parent_id == parent_id) {
  386. Tools.alert("文件已存在!");
  387. paste(oprate_param);
  388. } else {
  389. Tools.alert("正在黏贴");
  390. paste(oprate_param);
  391. }
  392. }
  393. //oprate_param.can_paste = false;
  394. //init($("#navigation").val(),3);
  395. },*/
  396. 'flush': function(t) {
  397. //刷新
  398. init($("#navigation").val(), 3);
  399. },
  400. 'sort': function(t) {
  401. init($("#navigation").val(), 4);
  402. },
  403. 'upload': function(t) {
  404. //上传文件
  405. var $M8610F002 = $("#M8610F002");
  406. K.form.reset($M8610F002);
  407. K.field.value($('#upload_id'), $("#navigation").val());
  408. K.popup($("#M8610P002"));
  409. }
  410. }
  411. });
  412. }
  413. //加载文件夹和文件
  414. function load() {
  415. var parentid = $("#navigation").val(),
  416. rows = select("M8610EQ006",{"id":parentid});
  417. $("#divall").empty();
  418. if(rows.length > 0){
  419. var str = "";
  420. for(var i = 0; i < rows.length; i++){
  421. if(rows[i].is_directory == "1"){
  422. str += "<li class='folder' title='" + rows[i].folder_name + "' index='" + i + "'><input type='text' class='changename' value='";
  423. str += rows[i].folder_name;
  424. str += "' data-id='" + rows[i].id + "' disabled='disabled' data-last-value='" + rows[i].folder_name + "'/></li>";
  425. }else if(rows[i].is_directory == "0"){
  426. var doc_fullname = rows[i].folder_name,
  427. doc_name = doc_fullname.substring(0, doc_fullname.lastIndexOf('.')),
  428. doc_type = doc_fullname.substring(doc_fullname.lastIndexOf('.') + 1),
  429. doc_type_class = $.inArray(doc_type, ["doc", "docx", "xls", "xlsx", "pdf"]) != -1 ? doc_type : "other-filetype";
  430. str += "<li class='file " + doc_type_class + "' title='" + rows[i].folder_name + "' index='" + i + "'><input type='text' class='changename' value='";
  431. str += doc_name;
  432. str += "' data-id='" + rows[i].id + "' data-filetype='" + doc_type + "' disabled='disabled' data-last-value='" + rows[i].folder_name + "'/></li>";
  433. }
  434. }
  435. $("#divall").append(str);
  436. }
  437. }
  438. function info(mode, id) {
  439. var str = "";
  440. if (mode == 1) { //展示目录下的对象数目
  441. $("#info-bar").empty();
  442. str += '<div class="folder info-icon"></div>';
  443. str += '<div class="info-detail"><form class="detail-form"><div class="detail-field detail-field2" ><span><var class="detail-var">';
  444. str += $("#divall").children("li").length;
  445. str += '</var>个对象</span></div>';
  446. str += '</form></div>';
  447. $("#info-bar").append(str);
  448. } else if (mode == 2) {
  449. $("#info-bar").empty();
  450. var rows = select("M8610EQ008",{"id":id});
  451. if(rows.length > 0){
  452. var row = rows[0],
  453. folder_name = row.folder_name,
  454. is_directory = row.is_directory,
  455. file_type = is_directory == "1" ? "" : folder_name.substring(folder_name.lastIndexOf('.') + 1),
  456. file_type_class = file_type == "" ? "folder" : ($.inArray(file_type, ["doc", "docx", "xls", "xlsx", "pdf"]) != -1 ? file_type : "other-filetype"),
  457. file_type_info = is_directory == "1" ? "文件夹" : folder_name.substring(folder_name.lastIndexOf('.') + 1) + "文件",
  458. crt_username = row.crt_username,
  459. upd_username = row.upd_username,
  460. crt_date = row.crt_date,
  461. crt_time = row.crt_time,
  462. upd_date = row.upd_date,
  463. upd_time = row.upd_time;
  464. str += '<div class="' + file_type_class + ' info-icon"></div><div class="info-detail"><form class="detail-form"><div class="detail-field" ><label>文件名:</label><span>';
  465. str += folder_name;
  466. str += '</span></div><div class="detail-field" ><label>创建人:</label><span>';
  467. str += crt_username != null ? crt_username : '';
  468. str += '</span></div><div class="detail-field" ><label>修改人:</label><span>';
  469. str += upd_username != null ? upd_username : '';
  470. str += '</span></div><div class="detail-field" ><label>文件类型:</label><span>';
  471. str += file_type_info;
  472. str += '</span></div><div class="detail-field" ><label>创建时间:</label><span>';
  473. str += crt_date != "" && crt_date != null ? crt_date.substring(0, 4) + '/' + crt_date.substring(4, 6) + "/" + crt_date.substring(6, 8) : "";
  474. str += crt_time != "" && crt_time != null ? " " + crt_time.substring(0, 2) + ":" + crt_time.substring(2, 4) + ":" + crt_time.substring(4, 6) : "";
  475. str += '</span></div><div class="detail-field" ><label>修改时间:</label><span>';
  476. str += upd_date != "" && upd_date != null ? upd_date.substring(0, 4) + '/' + upd_date.substring(4, 6) + "/" + upd_date.substring(6, 8) : "";
  477. str += upd_time != "" && upd_time != null ? " " + upd_time.substring(0, 2) + ":" + upd_time.substring(2, 4) + ":" + upd_time.substring(4, 6) : "";
  478. str += '</span></div></form></div>';
  479. $("#info-bar").append(str);
  480. }
  481. } else if (mode == 3) {
  482. $("#info-bar").empty();
  483. str += '<div class="folder info-icon"></div>';
  484. str += '<div class="info-detail"><form class="detail-form"><div class="detail-field detail-field2" ><span>已选中<var class="detail-var">';
  485. str += $("#divall").children("li.focus").length;
  486. str += '</var>个对象</span></div>';
  487. str += '</form></div>';
  488. $("#info-bar").append(str);
  489. }
  490. }
  491. function navigation(parent_id) {
  492. $("#navigation").val(parent_id);
  493. //查询文件路径
  494. var id = parent_id,
  495. flag = true,
  496. path = [],
  497. str = "";
  498. do {
  499. var rows = select("M8610EQ005",{"id":id});
  500. if(rows.length > 0){
  501. var row = rows[0];
  502. if(row.parent_id != 0){
  503. path.unshift({
  504. "folder_name": row.folder_name,
  505. "parent_id": id
  506. });
  507. id = row.parent_id;
  508. }else{
  509. flag = false;
  510. path.unshift({
  511. "folder_name": row.folder_name,
  512. "parent_id": id
  513. });
  514. }
  515. }
  516. } while (flag);
  517. $("#folder-navigation").empty();
  518. for (var i = 0; i < path.length; i++) {
  519. str += '<a class="foldername" data-id="' + path[i].parent_id + '">' + path[i].folder_name + '</a>';
  520. if (i != path.length - 1) {
  521. str += '<img class="triangle" src="images/triangle.png"/>';
  522. }
  523. }
  524. $("#folder-navigation").append(str);
  525. }
  526. function paste(param) {
  527. Util.ajaxRequest({
  528. url: "pasteDocManage.json",
  529. params: param,
  530. async: false,
  531. afterSuccess: function(json) {
  532. alert(3);
  533. var msg = json.returnmsg;
  534. var success = json.success;
  535. if (success == false) {
  536. if (msg == "windows") {
  537. Tools.alert("请检查windows的文档上传路径配置是否正确!");
  538. }
  539. }
  540. if (success == true) {
  541. }
  542. return false;
  543. }
  544. }, false);
  545. }
  546. function drag() {
  547. $("#divall li").each(function(i) {
  548. //$(".item_content .item").each(function(i) {
  549. this.init = function() { // 初始化
  550. this.box = $(this);
  551. console.log("left: " + this.box.offset().left + " top: " + this.box.offset().top);
  552. $(this).attr("index", i);
  553. /*.css({
  554. position : "absolute",
  555. left : this.box.offset().left,
  556. top : this.box.offset().top
  557. }).appendTo("#divall")*/
  558. this.drag();
  559. },
  560. this.move = function(callback) { // 移动
  561. $(this).stop(true).animate({
  562. left: this.box.offset().left,
  563. top: this.box.offset().top
  564. }, 500, function() {
  565. if (callback) {
  566. callback.call(this);
  567. }
  568. });
  569. },
  570. this.collisionCheck = function() {
  571. var currentItem = this;
  572. var direction = null;
  573. $(this).siblings(".item").each(function() {
  574. if (
  575. currentItem.pointer.x > this.box.offset().left &&
  576. currentItem.pointer.y > this.box.offset().top &&
  577. (currentItem.pointer.x < this.box.offset().left + this.box.width()) &&
  578. (currentItem.pointer.y < this.box.offset().top + this.box.height())
  579. ) {
  580. // 返回对象和方向
  581. if (currentItem.box.offset().top < this.box.offset().top) {
  582. direction = "down";
  583. } else if (currentItem.box.offset().top > this.box.offset().top) {
  584. direction = "up";
  585. } else {
  586. direction = "normal";
  587. }
  588. this.swap(currentItem, direction);
  589. }
  590. });
  591. },
  592. this.swap = function(currentItem, direction) { // 交换位置
  593. if (this.moveing) return false;
  594. var directions = {
  595. normal: function() {
  596. var saveBox = this.box;
  597. this.box = currentItem.box;
  598. currentItem.box = saveBox;
  599. this.move();
  600. $(this).attr("index", this.box.index());
  601. $(currentItem).attr("index", currentItem.box.index());
  602. },
  603. down: function() {
  604. // 移到上方
  605. var box = this.box;
  606. var node = this;
  607. var startIndex = currentItem.box.index();
  608. var endIndex = node.box.index();;
  609. for (var i = endIndex; i > startIndex; i--) {
  610. var prevNode = $(".item_container .item[index=" + (i - 1) + "]")[0];
  611. node.box = prevNode.box;
  612. $(node).attr("index", node.box.index());
  613. node.move();
  614. node = prevNode;
  615. }
  616. currentItem.box = box;
  617. $(currentItem).attr("index", box.index());
  618. },
  619. up: function() {
  620. // 移到上方
  621. var box = this.box;
  622. var node = this;
  623. var startIndex = node.box.index();
  624. var endIndex = currentItem.box.index();;
  625. for (var i = startIndex; i < endIndex; i++) {
  626. var nextNode = $(".item_container .item[index=" + (i + 1) + "]")[0];
  627. node.box = nextNode.box;
  628. $(node).attr("index", node.box.index());
  629. node.move();
  630. node = nextNode;
  631. }
  632. currentItem.box = box;
  633. $(currentItem).attr("index", box.index());
  634. }
  635. };
  636. directions[direction].call(this);
  637. },
  638. this.drag = function() { // 拖拽
  639. var oldPosition = new Position();
  640. var oldPointer = new Pointer();
  641. var isDrag = false;
  642. var currentItem = null;
  643. $(this).mousedown(function(e) {
  644. e.preventDefault();
  645. oldPosition.left = this.box.offset().left;
  646. oldPosition.top = this.box.offset().top;
  647. console.log("oldleft" + oldPosition.left + "oldtop" + oldPosition.top);
  648. oldPointer.x = e.clientX;
  649. oldPointer.y = e.clientY;
  650. isDrag = true;
  651. currentItem = this;
  652. });
  653. /*$(document).mousemove(function(e) {
  654. var currentPointer = new Pointer(e.clientX, e.clientY) ;
  655. if(!isDrag) return false ;
  656. $(currentItem).css({
  657. "opacity" : "0.8",
  658. "z-index" : 999
  659. }) ;
  660. var left = currentPointer.x - oldPointer.x + oldPosition.left ;
  661. var top = currentPointer.y - oldPointer.y + oldPosition.top ;
  662. $(currentItem).css({
  663. left : left,
  664. top : top
  665. }) ;
  666. currentItem.pointer = currentPointer ;
  667. // 开始交换位置
  668. //currentItem.collisionCheck() ;
  669. }) ;
  670. $(document).mouseup(function() {
  671. if(!isDrag) return false ;
  672. isDrag = false ;
  673. currentItem.move(function() {
  674. $(this).css({
  675. "opacity" : "1",
  676. "z-index" : 0
  677. }) ;
  678. }) ;
  679. }) ;*/
  680. };
  681. this.init();
  682. });
  683. }
  684. function leftClick() {
  685. //点击文件夹
  686. $("#divall li").click(function(event) {
  687. var $this = $(this),
  688. folder_name = $this.children("input.changename"),
  689. index = $this.attr("index");
  690. if (is_ctrl_down == true && is_shift_down == false) { //按下ctrl
  691. event.stopPropagation();
  692. focus_index = index;
  693. $("#divall").find("li").each(function(index) {
  694. $(this).children("input.changename").attr("disabled", "disabled");
  695. });
  696. if ($this.hasClass("focus")) {
  697. $this.removeClass("focus");
  698. } else {
  699. $this.addClass("focus");
  700. }
  701. info(3, 0);
  702. } else if (is_ctrl_down == false && is_shift_down == true) { //按下shift
  703. event.stopPropagation();
  704. if (focus_index == -1) {
  705. focus_index = index;
  706. $this.addClass("focus");
  707. } else {
  708. var index_min = Math.min(index, focus_index),
  709. index_max = Math.max(index, focus_index);
  710. $("#divall").find("li").each(function(i) {
  711. var i_index = $(this).attr("index");
  712. $(this).removeClass("focus");
  713. $(this).children("input.changename").attr("disabled", "disabled");
  714. if (i_index >= index_min && i_index <= index_max) {
  715. $(this).addClass("focus");
  716. }
  717. });
  718. }
  719. info(3, 0);
  720. } else {
  721. event.stopPropagation();
  722. $("#divall").find("li").each(function(index) {
  723. $(this).removeClass("focus");
  724. $(this).children("input.changename").attr("disabled", "disabled");
  725. });
  726. $this.addClass("focus");
  727. focus_index = index;
  728. clearTimeout(timeOutFn);
  729. timeOutFn = setTimeout(function() {
  730. folder_name.removeAttr("disabled");
  731. info(2, folder_name.attr("data-id"));
  732. }, 300);
  733. }
  734. });
  735. //点击文件名称
  736. /*$("#divall li input.changename").click(function(event) {
  737. if (is_ctrl_down == false) { //没有按下ctrl
  738. event.stopPropagation();
  739. console.log("input click");
  740. }
  741. });*/
  742. //点击空白的地方
  743. $("#all_folder").click(function() {
  744. console.log("blank click");
  745. $("#divall").find("li").each(function(index) {
  746. $(this).removeClass("focus");
  747. $(this).children("input.changename").attr("disabled", "disabled");
  748. });
  749. info(1, 0);
  750. });
  751. //点击后退按钮
  752. $("button.backward").off("click").click(function() {
  753. console.log("backward click");
  754. var currNode = find_active_node();
  755. if (currNode.previous.previous != null) {
  756. var preNode = currNode.previous,
  757. parent_id = preNode.element.parent_id;
  758. init(parent_id, 7);
  759. }
  760. });
  761. //点击前进按钮
  762. $("button.forward").off("click").click(function() {
  763. console.log("forward click");
  764. var currNode = find_active_node();
  765. if (currNode.next != null) {
  766. var nextNode = currNode.next,
  767. parent_id = nextNode.element.parent_id;
  768. init(parent_id, 8);
  769. }
  770. });
  771. //点击主页按钮
  772. $("button.home").off("click").click(function() {
  773. console.log("home click");
  774. if ($("#navigation").val() != 1) {
  775. init(1, 9);
  776. }
  777. });
  778. //点击返回上级目录
  779. $("button.gotopre").off("click").click(function() {
  780. console.log("gotopre click");
  781. if ($("#navigation").val() != 1) {
  782. //查询上级目录的parent_id
  783. var rows = select("M8610EQ005",{"id": $("#navigation").val()});
  784. if(rows.length > 0){
  785. var parent_id = rows[0].parent_id;
  786. if (parent_id != 0) {
  787. init(parent_id, 10);
  788. }
  789. }
  790. }
  791. });
  792. //点击地址栏地址
  793. $("a.foldername").off("click").click(function() {
  794. var parent_id = $(this).attr("data-id");
  795. if ($("#navigation").val() != parent_id) {
  796. init(parent_id, 11);
  797. }
  798. });
  799. }
  800. function focus() {
  801. $("#divall li input.changename").focus(function() {
  802. console.log("input focus");
  803. });
  804. $("#divall li").focus(function() {
  805. console.log("li focus");
  806. });
  807. }
  808. function blur() {
  809. $("#divall li").blur(function() {
  810. console.log("li blur");
  811. });
  812. $("#divall li input.changename").blur(function() {
  813. console.log("input blur");
  814. $(this).attr("disabled", "disabled");
  815. });
  816. }
  817. function change() {
  818. $("#divall li input.changename").change(function() {
  819. console.log("input change");
  820. var folder = $(this).parent("li"),
  821. data_last_value = $(this).attr("data-last-value"),
  822. folder_name = $(this).val(),
  823. id = $(this).attr("data-id"),
  824. is_directory = folder.hasClass("folder") ? 1 : 0,
  825. doc_type = folder.hasClass("folder") ? "" : $(this).attr("data-filetype"),
  826. parent_id = $("#navigation").val(),
  827. params = {
  828. "folder_name": folder_name,
  829. "id": id,
  830. "is_directory": is_directory,
  831. "doc_type": doc_type,
  832. "parent_id": parent_id,
  833. "description": ""
  834. };
  835. if (update_folder_name(params)) {
  836. $(this).attr("data-last-value", folder_name);
  837. info(2, $(this).attr("data-id"));
  838. } else {
  839. $(this).val(data_last_value);
  840. }
  841. });
  842. }
  843. function dbclick() {
  844. /* $("#divall li.folder").dblclick(function() {
  845. clearTimeout(timeOutFn);
  846. console.log("li dblclick");
  847. var folder = $(this).children("input.changename");
  848. init(folder.attr("data-id"), 6);
  849. });*/
  850. }
  851. function keydown() {
  852. $(document).keydown(function(event) {
  853. if (event.which == '17') {
  854. is_ctrl_down = true;
  855. } else if (event.which == '16') {
  856. is_shift_down = true;
  857. }
  858. });
  859. }
  860. function keyup() {
  861. $(document).keyup(function(event) {
  862. if (event.which == '17') {
  863. is_ctrl_down = false;
  864. } else if (event.which == '16') {
  865. is_shift_down = false;
  866. }
  867. });
  868. }
  869. //新增目录
  870. var add_folder = function(params) {
  871. var flag = false;
  872. if (!testFolderName(params.folder_name)) {
  873. Tools.alert("文件夹名不能包括\\\/:*?\"<>|等特殊符号");
  874. return flag;
  875. }
  876. if (judgeDocExist(params.id, params.folder_name)) {
  877. Tools.alert("该目录已存在,不能添加");
  878. return flag;
  879. }
  880. var param = {
  881. description: params.description,
  882. folder_name: params.folder_name,
  883. is_directory: 1,
  884. parent_id: params.id,
  885. port_level: 1,
  886. username: "semitree",
  887. date: new Date().Format("yyyyMMdd"),
  888. time: new Date().Format("hhmmss")
  889. };
  890. insert("M8610ES001",param);
  891. return true;
  892. };
  893. //修改文件名称
  894. var update_folder_name = function(params) {
  895. var folder_name = "",
  896. flag = false;
  897. if (params.is_directory == 1) {
  898. folder_name = params.folder_name;
  899. } else {
  900. folder_name = params.folder_name + '.' + params.doc_type;
  901. }
  902. if (!testFolderName(params.folder_name)) {
  903. Tools.alert("文件名不能包括\\\/:*?\"<>|等特殊符号");
  904. return flag;
  905. }
  906. if (judgeDocUpdate(params.id, folder_name)) {
  907. Tools.alert("该目录/文档已存在,请重新输入!");
  908. return flag;
  909. }
  910. var current_date = new Date(),
  911. date = current_date.Format("yyyyMMdd"),
  912. time = current_date.Format("hhmmss");
  913. update("M8610EU001",{"description":params.description,
  914. "folder_name":params.folder_name,
  915. "username":"semitree",
  916. "date":date,
  917. "time":time,
  918. "id":params.id
  919. });
  920. flag = true;
  921. // K.popup.close($("#M8610P001"));
  922. //修改文件名
  923. $("#divall").find("input[data-id=" + params.id + "]").val(params.folder_name);
  924. return flag;
  925. };
  926. //上传文档
  927. var uploadFile = function(params) {
  928. var $need_appendix = $('#M8610F002').find('input[name=need_appendix]'),
  929. fileList = $need_appendix.get(0).files,
  930. fileNameWithSuffixList = [],
  931. fileNameList = [];
  932. $.each(fileList,function(index,file){
  933. var name = file.name;
  934. fileNameWithSuffixList.push(name);
  935. fileNameList.push(name.substring(0, name.lastIndexOf('.')));
  936. });
  937. if(fileNameWithSuffixList.length == 0){
  938. $.pt({
  939. target: $need_appendix,
  940. position: 'r',
  941. align: 't',
  942. width: 'auto',
  943. height: 'auto',
  944. content:"请先选择文件"
  945. });
  946. return;
  947. }else{
  948. $.each(fileNameList,function(index,fileName){
  949. //文件名真实长度不能超过100
  950. var blen = 0;
  951. for (var i = 0; i < fileName.length; i++) {
  952. if ((fileName.charCodeAt(i) & 0xff00) != 0) {
  953. blen++;
  954. }
  955. blen++;
  956. }
  957. if (blen > 100) {
  958. $.pt({
  959. target: $need_appendix,
  960. position: 'r',
  961. align: 't',
  962. width: 'auto',
  963. height: 'auto',
  964. content:"文件名过长!(支持50个中文或100个英文)"
  965. });
  966. console.log("文件名过长!(支持50个中文或100个英文)");
  967. return false;
  968. }
  969. });
  970. $.each(fileNameWithSuffixList,function(index,docName){
  971. var param = {
  972. description: "",
  973. folder_name: docName,
  974. is_directory: 0,
  975. parent_id: params.id,
  976. port_level: 1,
  977. username: "semitree",
  978. date: new Date().Format("yyyyMMdd"),
  979. time: new Date().Format("hhmmss")
  980. };
  981. insert("M8610ES001",param);
  982. });
  983. }
  984. init($("#navigation").val(), 1);
  985. K.popup.close($("#M8610P002"));
  986. return true;
  987. };
  988. var dele = function(params) {
  989. //查询该目录下是否存在子目录/文档
  990. var rows = select("M8610EQ004",{"id":params.id}),
  991. desc = rows.length > 0 ? "删除整个文件夹(包含所有子目录和子文档)?" : "确认删除?";
  992. Tools.confirm(desc, function(ok) {
  993. if (ok) {
  994. deleteDoc(params);
  995. init($("#navigation").val(), 2);
  996. }
  997. });
  998. };
  999. //删除文档或目录
  1000. var deleteDoc = function(params) {
  1001. var is_directory = params.is_directory,
  1002. id = params.id;
  1003. if(is_directory == 0){//删除单个文件
  1004. del("M8610ED001",{"id":id});
  1005. }else{//删除文件夹
  1006. //递归删除文件夹下所有的子文件和文件夹
  1007. var rows = select("M8610EQ006",{"id":id});
  1008. if(rows.length > 0){
  1009. for(var i = 0; i < rows.length; i++){
  1010. deleteDoc(rows[i]);
  1011. }
  1012. }
  1013. del("M8610ED001",{"id":id});
  1014. }
  1015. };
  1016. //打包下载文件夹或文件
  1017. var download = function(params) {
  1018. var $M8610F003 = $("#M8610F003");
  1019. K.form.reset($M8610F003);
  1020. K.field.value($('#_id'), params.id);
  1021. K.field.value($('#p_id'), params.parent_id);
  1022. K.field.value($('#f_name'), params.folder_name);
  1023. K.field.value($('#i_directory'), params.is_directory);
  1024. if (params.is_directory == 1) {
  1025. Tools.confirm("是否打包下载整个文件夹?(可能需要较长时间,请耐心等待)", function(ok) {
  1026. if (ok) {
  1027. //判断所下载的目录是否为空
  1028. Util.ajaxRequest({
  1029. url: "directoryIsNull.json",
  1030. params: params,
  1031. async: false,
  1032. afterSuccess: function(json) {
  1033. var msg = json.returnmsg;
  1034. if (msg == "目录为空") {
  1035. Tools.alert("请不要下载空目录");
  1036. }
  1037. if (msg == "目录不为空") {
  1038. K.submit($('#M8610F003'), null, true);
  1039. }
  1040. return false;
  1041. }
  1042. }, false);
  1043. }
  1044. });
  1045. } else {
  1046. K.submit($('#M8610F003'), null, true);
  1047. }
  1048. };
  1049. var testFolderName = function(folderName) {
  1050. var reg = new RegExp('^[^\\\\\\/:*?\\"<>|]+$');
  1051. return reg.test(folderName);
  1052. };
  1053. //同一父目录下不能有同名目录或同名文档
  1054. function judgeDocUpdate(id, name) {
  1055. var pd = false;
  1056. /*var rows = select("M8610EQ002",{"id":id,"folder_name":name});
  1057. if(rows.length > 0){
  1058. if (rows[0].count > 0) {
  1059. pd = true;
  1060. } else {
  1061. pd = false;
  1062. }
  1063. }*/
  1064. return pd;
  1065. };
  1066. //新增目录、上传文档时校验同目录下是否有同名目录/文档
  1067. function judgeDocExist(id, folder_name) {
  1068. var pd = false;
  1069. /*var rows = select("M8610EQ003",{"id":id,"folder_name":folder_name});
  1070. if(rows.length > 0){
  1071. if (rows[0].count > 0) {
  1072. pd = true;
  1073. } else {
  1074. pd = false;
  1075. }
  1076. }*/
  1077. return pd;
  1078. }
  1079. //选择文件时动态加载信息框
  1080. function select_file(ele){
  1081. var $this = $(ele),
  1082. fileList = $this.get(0).files,
  1083. fileNameList = [],
  1084. fieldsetNameList = [];
  1085. //获取所有选择文件的文件名
  1086. for(var i = 0; i < fileList.length; i++){
  1087. fileNameList.push(fileList[i].name);
  1088. }
  1089. //获取所有已有的图片fieldset名
  1090. $("#M8610F002").children("fieldset.picture-fieldset").each(function(){
  1091. var $this = $(this),
  1092. $legend = $this.children("legend"),
  1093. legend_name = $legend.text();
  1094. fieldsetNameList.push(legend_name);
  1095. });
  1096. for(var index in fieldsetNameList){
  1097. if($.inArray(fieldsetNameList[index],fileNameList) == -1){
  1098. $("#M8610F002").find("legend.picture-legend:contains("+fieldsetNameList[index]+")").parent().remove();
  1099. }
  1100. }
  1101. var first = true;
  1102. $.each(fileNameList,function(index,fileName){
  1103. if($.inArray(fileName,fieldsetNameList) == -1){
  1104. //添加一个对应的fieldset
  1105. var html = "",checked = "";
  1106. if(first){
  1107. checked = "checked";
  1108. }
  1109. html += '<fieldset class="single-fieldset picture-fieldset">'+
  1110. '<legend class="picture-legend">'+fileName+'</legend>'+
  1111. '<input class="hide" name="picture_name" value="'+fileName+'">'+
  1112. '<div>'+
  1113. '<label class="my-label">标题:</label>'+
  1114. '<input class="my-input" type="text" name="picture_title" placeholder="请输入图片标题" maxlength="16"/>'+
  1115. '<input type="radio" '+checked+' name="cover" value="'+fileName+'">设为封面'+
  1116. '</div><div>'+
  1117. '<label class="my-textarea-label">描述:</label>'+
  1118. '<textarea class="my-textarea" name="picture_desc" placeholder="请输入图片描述" maxlength="100"></textarea>'+
  1119. '</div></fieldset>';
  1120. $("#M8610F002").append(html);
  1121. textarea_bind();
  1122. first = false;
  1123. }
  1124. });
  1125. //K.init($("#M8610F002"));
  1126. }
  1127. function textarea_bind(){
  1128. $("#M8610F002").find("textarea").each(function(index){
  1129. $(this).unbind('input').bind('input',function(){
  1130. var self =this,
  1131. maxLength = parseInt($(this).attr("maxlength")),
  1132. curLength = $(this).val().length,
  1133. span_html = "";
  1134. span_html += '<span><var class="word">'+(maxLength-curLength)+'</var>/'+maxLength+'</span>';
  1135. console.log(span_html);
  1136. $.pt({
  1137. target: self,
  1138. position: 'r',
  1139. align: 't',
  1140. width: 'auto',
  1141. height: 'auto',
  1142. content:span_html
  1143. });
  1144. });
  1145. });
  1146. }
  1147. /*
  1148. * 定义拖动类
  1149. */
  1150. function Pointer(x, y) {
  1151. this.x = x;
  1152. this.y = y;
  1153. }
  1154. function Position(left, top) {
  1155. this.left = left;
  1156. this.top = top;
  1157. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号