先上张效果图吧

1.引入dropzone的js和css文件

2.html这里我用了一个form,当然你也可以直接用一个div,无论你用什么都要加上class="dropzone"

3.js

             var fileArr = new Array();
jQuery(function($){
Dropzone.autoDiscover = false;
Dropzone.options.myAwesomeDropzone = false;
try {
$(".dropzone").dropzone({
url:"${pageContext.request.contextPath}/uploadController/upload.action",
method:"post",
paramName:"file",
autoProcessQueue:true,//自动上传
maxFilesize:1024, // MB
uploadMultiple:false,
parallelUploads:10,
acceptedFiles:".rar,.zip,.7z",
dictInvalidFileType:"支持的文件类型是.rar,.zip,.7z",
addRemoveLinks:true,
// maxFiles: //指的是上传目录下的最大文件数
dictRemoveFile:"移除文件",
dictUploadCanceled:"取消",
dictCancelUploadConfirmation:"取消上传该文件?",
dictDefaultMessage:
"<span class='bigger-150 bolder'><i class='icon-caret-right red'></i>拖动文件</span>上传\
<span class='smaller-80 gre'>(或者点击上传)</span> <br /> \
<i class='upload-icon icon-cloud-upload blue icon-3x'></i>",
dictResponseError:"文件上传失败!",
dictFileTooBig:"文件过大,上传失败!",
//change the previewTemplate to use Bootstrap progress bars
previewTemplate: "<div class=\"dz-preview dz-file-preview\">\n <div class=\"dz-details\">\n <div class=\"dz-filename\"><span data-dz-name></span></div>\n <div class=\"dz-size\" data-dz-size></div>\n <img data-dz-thumbnail />\n </div>\n <div class=\"progress progress-small progress-striped active\"><div class=\"progress-bar progress-bar-success\" data-dz-uploadprogress></div></div>\n <div class=\"dz-success-mark\"><span></span></div>\n <div class=\"dz-error-mark\"><span></span></div>\n <div class=\"dz-error-message\"><span data-dz-errormessage></span></div>\n</div>",
init:function(){
this.on("addedfile",function(file,data) {
fileArr.push(file.upload.uuid);
//解决点击时重复发送请求
$(".dz-remove").each(function(index) {
if(!$(".dz-remove:eq(" + index + ")").attr("id")) {
$(".dz-remove:eq(" + index + ")").attr("id",fileArr[index]);
}
}) })
this.on("success",function(file,data){
//var myDropzone = this;
$("#" + file.upload.uuid).click(function() {
var fileName = $(this).parent().find(".dz-filename").text();
if(window.confirm("确定要删除吗?")) {
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/uploadController/delete.action",
data:{"fileName":fileName},
dataType:"json",
success:function(data){
// this.removeFile(file);
}
})
} }) });
this.on("sending",function(file,data){ })
this.on("removedfile",function(file,data){ }) this.on("canceled",function(file,data) {
// alert("canceled");
this.removeFile(file);
}); this.on("error",function(file,data){ });
this.on("complete",function(file) {
if(file.status == "canceled" || file.status == "error") {
var fileName = $("#" + file.upload.uuid).parent().find(".dz-filename").text();
setTimeout(function() {
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/uploadController/delete.action",
data:{"fileName":fileName},
dataType:"json",
success:function(data){
if(data == "success") {
// alert("删除成功");
}
},
error:function(ajax) {
alert(ajax.status);
}
})
},2000);
}
}) }
});
} catch(e) {
alert('Dropzone.js does not support older browsers!');
} });

注意事项:

1.关于parallelUploads,这个参数我看了很多博客,都没有介绍,于是我去官网查了下,发现这个参数是文件上传队列数量的意思,

什么意思呢?如果你设置为1,但你上传的时候选择了多个文件,那么这些文件只会1个1个的上传而不是多个同时上传

3.后台

如果你做的时候后台报了异常org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface,

请在MultipartFile参数前加上@RequestParam,关于这个注解是起什么作用,自行百度

接收文件

 @RequestMapping("/upload")
public String upload(@RequestParam MultipartFile file,HttpSession session){
if(file == null) {
return "";
}
File newFile = null;
InputStream is = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
String originalFilename = file.getOriginalFilename();
byte[] buffer = new byte[1024];
String dirPath = session.getServletContext().getRealPath("/") + "upload";
File dir = new File(dirPath);
if(!dir.exists()) {
dir.getParentFile().mkdirs();
}
if(originalFilename != null && originalFilename.trim().length() > 0) {
newFile = new File(dirPath + "/" + originalFilename);
}
bos = new BufferedOutputStream(new FileOutputStream(newFile));
is = file.getInputStream(); bis = new BufferedInputStream(is);
int count = 0;
while((count = bis.read(buffer)) != -1){ bos.write(buffer, 0,count);
}
bos.flush(); String createTime = df.format(System.currentTimeMillis());
FileBean fileBean = fileBeanService.findByName(originalFilename);
if(fileBean == null) {
fileBean = new FileBean();
fileBean.setName(originalFilename);
}
fileBean.setCreateTime(df.parse(createTime));
fileBeanService.add(fileBean); } catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
return "redirect:/uploadController/dropzone.action";
}

2.关于移除和取消上传文件

如果你用了数据库,直接把对应的字段改成0就表示此文件不存在,可以不删除如果你打算真的删除时,执行delete方法前后尽量先让线程睡眠一段时间,否则容易引起IOException,事实上文件上传过程中点击取消,实现的思路是先把文件上传上来,然后再删除,直接执行删除也有可能引起IOException

ps:这也是3月初的时候用的插件,至今过了一个月了才抽出时间写写总结,在此记录下来给自己一个参考

dropzone上传文件的更多相关文章

  1. django + dropzone.js 上传文件

    1.dropzone.js http://www.dropzonejs.com/ dropzone.js是一个可预览\可定制化的文件拖拽上传,实现AJAX异步上传文件的工具 2.dropzone.js ...

  2. 上传文件插件dropzone的实例

    html: <div class="field"> <div id="file" class="dropzone"> ...

  3. FTP上传文件到服务器

    一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...

  4. dropzone 上传插件

    dropzone dropzone.js是一个可预览的上传文件工具,不依赖任何框架(如jQuery),且具有可定制化.实现文件拖拽上传,提供AJAX异步上传功能. 1. html文件 dropzone ...

  5. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  6. 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader

    发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...

  7. asp.net mvc 上传文件

    转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...

  8. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

  9. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

随机推荐

  1. 洛谷 P2025 脑力大人之监听电话

    P2025 脑力大人之监听电话 题目背景 画外音: (声明:不要管前面那个,纯属意外,现已经重新编题,绝对原创) 上次海选,我们选出了参赛者中的20%参加本次比赛,现在我们将进行第二轮的筛选,这次的比 ...

  2. Traveler Nobita (zoj 3456 最小生成树)

    Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a time machin ...

  3. 删除dataGridview中选中的一行或多行

    一.实现的功能:可以删除一行或者多行数据,并在删除前提醒是否确定进行删除! DialogResult RSS = MessageBox.Show(this,"确定要删除选中行数据码?&quo ...

  4. 1.5 Python基础知识 - while循环

    在我们生活中有很多反复要做的事情,或者动作,我们称之为循环.在开发程序中也会有循环的事情要去做,就是需要反复的去执行某个代码,或者反复进行某种演算,直到达到某种条件的时候才会停止.在Python中我们 ...

  5. Nginx TCP代理

    nginx 在1.9.0 版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为nginx_tcp_proxy_module的第三方patch来实现,该模块的代码托管在github上 网址: ...

  6. 【Codeforces Round #433 (Div. 1) B】Jury Meeting

    [链接]h在这里写链接 [题意] 有n个人,它们都要在某一时刻开始,全都到达0位置,然后维持最少k个时间单位,然后再全都回到原来的位置; 第i个人初始的位置是i. 且一共有m班航班. 每一班航班,要么 ...

  7. comparator接口与Comparable接口的差别

    1. Comparator 和 Comparable 同样的地方 他们都是java的一个接口, 而且是用来对自己定义的class比較大小的, 什么是自己定义class: 如 public class  ...

  8. 关于stm32加不进.h文件的问题

    把路径也设置好了,但是.h文件加入不进去, 编译的时候.h文件也出来了 那是因为.h或对应的.c文件中存在错误,改掉错误就能成功,有时候keil不会报错,可能是因为定义变量没有定义好 如果显示某个变量 ...

  9. [React] Create an Auto Resizing Virtualized List with react-virtualized

    In this lesson we'll show how to use the AutoSizer component from react-virtualized to automatically ...

  10. FragmentPagerAdapter和FragmentStatePagerAdapter的差别

    ViewPager同意用户通过左右滑动显示不同页面的数据.而这些页面须要PagerAdapter管理. 经常使用的有FragmentPagerAdapter和FragmentStatePagerAda ...