java实习生一枚,前端知识薄弱,最近因为工作需要,做了一个拖拽文件上传的功能,发现dropzone.js挺不错的,特地做个笔记。

dropzonejs 的官网是:http://www.dropzonejs.com/, 中文手册是:http://wxb.github.io/dropzonejs.com.zh-CN/

自己写的拖拽文件至一个按钮上传的功能,前端及java代码如下:

jsp页面:

1. 首先必须引入dropzone的js和css文件

2.自己定义两个div区域

uopload

3.对dropzone.css进行修改,将文件内的所有dropzone替换为dropz

修改文件拖拽区域的显示样式:

.dropz {/*设置拖拽上传文件按钮的格式*/

min-height:0px;

min-width: 100px;

border: 1px solid #58AF0C;

background: white;

padding: 15px 20px;

background-color: #7AC143;

background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #7AC143),

color-stop(1, #7AC143));

background-position: center top;

background-repeat: no-repeat;

border-radius: 5px;

min-height:0px;

min-width: 100px;

padding: 15px 20px;

color: #FFF;

font: bold 12px Arial, Helvetica, sans-serif;

text-align: center;

text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);

}

.dropz.dz-clickable {

cursor: pointer;

line-height: 0px;/*按钮中的文字垂直居中*/

}

4.在jsp对div进行dropzone参数的自定义

$("#div1").dropzone({

url:"systemController.action?saveFile",//上传文件的地址,

maxFiles:1,//最多上传几个文件

maxFilesize: 5,//文件的大小,单位是M

addRemoveLinks:true,//是否有删除文件的功能

dictRemoveFile:"",//删除文件

previewsContainer:"#div2",//文件上传进度显示的区域

acceptedFiles: ".jpg,.jpeg,.png,.gif,.xls,.txt,.sql,.rar,.mkv",//支持的格式

paramName:'file',//上传的FILE名称,即服务端可以通过此来获取上传的文件,如$_FILES['dropimage']

init: function() {//初始化时的事件

//$("#uploadfile").uploadFile({success:function(data){

this.on("addedfile", function(file) {

// Create the remove button

var removeButton = Dropzone.createElement("");

// Capture the Dropzone instance as closure.

var _this = this;

// Listen to the click event

removeButton.addEventListener("click", function(e) {

// Make sure the button click doesn't submit the form:

e.preventDefault();

e.stopPropagation();

alert("Are you sure to delete?");

// Remove the file preview.

_this.removeFile(file);

// If you want to the delete the file on the server as well,

// you can do the AJAX request here.

});

// Add the button to the file preview element.

file.previewElement.appendChild(removeButton);

});

this.on("success", function(file, data) {

if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {

var d = $.parseJSON(data);

var fileitem = "" + d.name

+ "

+ d.fileKey + "','" + d.name

+ "') title='删除' src='plug-in/uploadify/img/uploadify-cancel.png' widht='15' height='15'>  ";

$("#fileslist").html(fileitem);

$("#attachment").val(d.fileKey + "," + d.name + ";");

}

this.removeFile(file);

});

}

});

java后台处理文件上传的代码:

@RequestMapping(params = "saveFile", method = RequestMethod.POST)

public void saveFile(HttpServletRequest request, HttpServletResponse response, TSDocument document) throws Exception{

Map attributes = new HashMap();

TSTypegroup tsTypegroup=systemService.getTypeGroup("fieltype","文档分类");

TSType tsType = systemService.getType("files","附件", tsTypegroup);

String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID

String documentTitle = oConvertUtils.getString(request.getParameter("documentTitle"),"uploadfile");// 文件标题

if (StringUtil.isNotEmpty(fileKey)) {

document.setId(fileKey);

document = systemService.getEntity(TSDocument.class, fileKey);

document.setDocumentTitle(documentTitle);

}

document.setBusinessKey(request.getParameter("businessKey"));

document.setSubclassname(MyClassLoader.getPackPath(document));

document.setCreatedate(DateUtils.gettimestamp());

document.setTSType(tsType);

UploadFile uploadFile = new UploadFile(request, document);

uploadFile.setCusPath("files");

uploadFile.setSwfpath("swfpath");

document = systemService.uploadFile(uploadFile);

attributes.put("url", document.getRealpath());

attributes.put("fileKey", document.getId());

if (ResourceUtil.getSessionUserName()!=null) {

attributes.put("uploadUser", ResourceUtil.getSessionUserName().getUserName());

}else{

attributes.put("uploadUser", "null");

}

attributes.put("time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

attributes.put("name", document.getAttachmenttitle()+"."+document.getExtend());

attributes.put("downloadurl", "commonController.action?viewFile&fileid="+ document.getId()+"&subclassname=");

attributes.put("viewhref", "commonController.action?objfileList&fileKey=" + document.getId());

attributes.put("delurl", "commonController.action?delObjFile&fileKey=" + document.getId());

attributes.put("realPath", document.getRealpath());

if(FileUtils.isPicture(document.getExtend())){

attributes.put("imgUrl", document.getRealpath());

}

JSONObject js = new JSONObject(attributes);

response.getWriter().write(js.toString());

response.getWriter().flush();

}

注意这里的返回值是直接返回的json对象,如果采用

@RequestMapping(params = "saveFiles", method = RequestMethod.POST)

@ResponseBody

则会报错:

[com.framework.core.common.exception.MyExceptionHandler]org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation最终实现的效果如下:

更多使用功能请参考dropzone的官方文档。

dropzone.js应用java_拖拽文件上传(Java篇)dropzone.js的简单使用相关推荐

  1. dropzone java实例_Java实现拖拽文件上传dropzone.js的简单使用示例代码

    Java实习生一枚,前端知识薄弱,最近因为工作需要,做了一个拖拽文件上传的功能,发现dropzone.js挺不错的,特地做个笔记. 自己写的拖拽文件至一个按钮上传的功能,前端及java代码如下: js ...

  2. php 拖动多个文件上传,dropzone拖拽文件上传一次上传多个文件的方法

    用dropzone插件拖拽文件上传默认情况是把一个文件拖到浏览器后就立即自动上传,参考前文<用dropzone插件拖拽文件上传>,如果需要一次上传多个文件怎么办?本文将介绍其实现方法. d ...

  3. HTML5拖拽文件上传

    上传文件 HTML5新增了文件API,提供客户端本地操作文件的可能. 我们可以通过file表单或拖放操作选择文件,还可以通过JavaScript读取文件的名称.大小.类型.和修改时间. file类型的 ...

  4. H5中的拖拽文件上传-----------------需修改,需测试

    一:介绍 1.内容摘要 2.主要设计的技术 3.drag与drop事件 4.drag与drop的部分重要代码 5.File Api 6.formData 二:程序演示 1. 1 <!DOCTYP ...

  5. ajax 批量上传图片插件,jQuery多文件上传插件jquery.imageuploader.js

    插件描述:jQuery多文件上传插件jquery.imageuploader.js,可以同时上传多个文件并支持拖拽上传 jquery.imageuploader.js 一款jquery多文件上传插件. ...

  6. vue-quill-editor图片大小的编辑,拖拽,上传视频

    vue-quill-editor图片大小的编辑,拖拽,上传视频 在vue组件中注册和使用 <quill-editor v-model="formInline.viewConfig&qu ...

  7. django文件——django + jquery-file-upload上传篇(二)-- 插件实现文件上传+进度条显示 +拖入文件上传...

    django + jquery-file-upload 插件实现文件上传+进度条显示 1.model.py class Fujian(models.Model):name = models.CharF ...

  8. Bootstrap fileinput.js,最好用的文件上传组件

    本篇介绍如何使用bootstrap fileinput.js(最好用的文件上传组件)来进行图片的展示,上传,包括springMVC后端文件保存. 一.demo   二.插件引入 <link ty ...

  9. aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决

    aliyun oss 文件上传 java.net.SocketTimeoutException Read timed out 问题分析及解决 参考文章: (1)aliyun oss 文件上传 java ...

最新文章

  1. python pexpect
  2. 关键字static、const、volatile的作用
  3. JavaScript内存释放笔记
  4. 期末复习、化学反应工程科目(第八、九章)
  5. 80%的人都不知道的排版利器,博士生都在用它!
  6. 【SpringBoot零基础案例09】【IEDA 2021.1】SpringBoot将核心配置文件中的自定义配置映射到一个对象
  7. ISA2006无人值守安装
  8. 光端机的分类有哪些?
  9. 线程自动退出_C++基础 多线程笔记(一)
  10. 研讨会 | “人工智能与行业知识图谱技术实战”研讨会
  11. fibonacci数列python_从 Python 计算 Fibonacci 数列说起
  12. c语言实现通讯录(详解)
  13. Ognl表达式(根据Apache-Ognl文档直译)
  14. CISSP-D7-运营安全
  15. 沧小海深入剖析xilinx的GTP/GTX核,掌握高速串行收发机制——第六章 接收端结构及功能说明
  16. Processing——码绘与手绘对比动态篇
  17. RxJava操作符学习APP
  18. 《TCP/IP详解》读书笔记(18章)-TCP连接的建立与中止
  19. 百度人脸识别SDK的坑
  20. (二)Landsat_5 TM 遥感影像波段合成真假彩色影像

热门文章

  1. AD画图全过程(看这一篇就够了)
  2. PB 利用timer()函数实现定时将数据窗口以excel文档保存至指定地方
  3. 汽车电子控制单元(ECU)的架构---DCU和MDC域控制器
  4. 通过 域控服务器 访问客户端“计算机管理”
  5. 硬盘基本知识(磁头、磁道、扇区、柱面
  6. C++中有符号与无符号
  7. 查询服务器的操作系统,查询服务器的操作系统
  8. 虎符CTF MISC 奇怪的组织
  9. 龙卷风路径_龙卷风路径图片壁纸 60 Tornado Alley - 猫猫壁纸酷 wallcoo.com
  10. 弘玑RPA x 流程挖掘,构筑超自动化的坚实底座