dropzone插件实现指定文件夹上传和多文件上传功能

  • 格式:docx
  • 大小:21.48 KB
  • 文档页数:9

Jsp:

默认文件夹

红色的地方标注:可以上传文件夹的参数

Js: //Dropzone实现上传文件 var AcceptedFilesLength = 0; var count = -1; var flag = true; Dropzone.options.myDropzone = {

//添加上传取消和删除预览的链接,默认不添加 addRemoveLinks: true,

//关闭自动上传功能,默认会true会自动上传

//也就是添加一个文件向服务器发送一次请求 autoProcessQueue: false,

//用于传输文件的名称 paramName: "myFile",

//允许上传多个照片 uploadMultiple: false,

//MB maxFilesize: 20,

//最大上传数量 maxFiles: 5,

dictFileTooBig: '图片超出最大20M约束',

dictMaxFilesExceeded: '超出最大上传数量',

//限制上传文件大小的节 parallelUploads: 100, init: function () { var submitButton = document.querySelector("#btn-cfmupload") myDropzone = this; // closure

//为上传按钮添加点击事件

submitButton.addEventListener("click", function () {

//手动上传所有文件 myDropzone.processQueue(); });

//当上传完成后的事件,接受的数据为JSON格式 this.on("complete", function (file) { myDropzone.removeFile(file);

//alert(this.getAcceptedFiles().length);

if(flag){ <%-- alert("a");--%> AcceptedFilesLength = this.getUploadingFiles().length;

flag = false; } if(count == AcceptedFilesLength){ count = -1; flag = true;

toast('上传成功!');

refreshLeftLibaryFolder(); Submit();

}

//alert("this.getUploadingFiles().length==" + this.getUploadingFiles().length);

//alert("this.getQueuedFiles().length==" + this.getQueuedFiles().length);

if(this.getRejectedFiles().length != 0){ count = -1;

Alert('上传失败'); return; } });

this.on("success", function(file,data) { var obj = JSON.parse(data); if(obj.result == 1){ count++; } });

} };

Action: /** * 将上传的文件保存到数据库 * @param model gll * @return */ @RequestMapping(value = "/insertfile",method=RequestMethod.POST) @ResponseBody public String insertfile(@RequestParam("myFile") CommonsMultipartFile myFile,HttpServletRequest request,HttpSession session){

JSONObject json = new JSONObject(); try { int result = 0; String listFolderId = request.getParameter("folderId"); String[] path=ListLibaryController.upload(myFile, request, Const.uploadFolder.listLibary.getValue(), listFolderId); if(null != path){ DecimalFormat df2 = new DecimalFormat("###.0"); String size = null;

if(Integer.valueOf(path[2])>0){

if(Integer.valueOf(path[2])/1024<1024){//如果小于1M size= df2.format(((double)Integer.valueOf(path[2])/(double)1024))+"Kb"; }else if(Integer.valueOf(path[2])/1024>=1024){//如果大于1M size = df2.format((Integer.valueOf(path[3])/(1024*1024)))+"MB"; }else if(Integer.valueOf(path[2])/1024>=1024*1024

){//如果大于1024MB size = df2.format((Integer.valueOf(path[2])/(1024*1024*1024)))+"G"; } } YtList ytlist=new YtList(); ytlist.setListName(path[0]);

ytlist.setListAddtime(new Date()); ytlist.setListPath(path[1]);

ytlist.setListSize(size); User user = (User)session.getAttribute(Const.SESSION_USER); user = userService.getUserAndRoleById(user.getUserId());

ytlist.setUserId(user.getUserId());

result=ytListService.insertSelective(ytlist);

savefolderLink(session,String.valueOf(ytlist.getListId()),Integer.valueOf(listFold

erId));//加入文件夹 }