先上张效果图吧

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. Android底层开发之Linux输入子系统要不要推断系统休眠状态上报键值

    Android底层开发之Linux输入子系统要不要推断系统休眠状态上报键值 题外话:一个问题研究到最后,那边记录文档的前半部分基本上都是没用的,甚至是错误的. 重点在最后,前边不过一些假想猜測. ht ...

  2. Notepad++和MinGW的安装和配置

    http://blog.csdn.net/cclovepl/article/details/70568313 http://blog.csdn.net/cclovepl/article/details ...

  3. (转) 25个必须记住的SSH命令

    转自:http://www.cnblogs.com/weafer/archive/2011/06/10/2077852.html OpenSSH是SSH连接工具的免费版本.telnet,rlogin和 ...

  4. 6.3 Android硬件访问服务APP代码

    以下步骤是操作MainActivity类 1.导入包 import android.os.ILedService 2.添加成员变量 private ILedService iLedService = ...

  5. [CSS] Use CSS Counters to Create Pure CSS Dynamic Lists

    CSS counters let you create dynamic lists without JavaScript. In this lesson, we will create a multi ...

  6. FragmentPagerAdapter和FragmentStatePagerAdapter的差别

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

  7. Apache多虚拟主机多版本PHP(5.3+5.6+N)共存运行配置全过程

    摘要: 为需要实现在同一台Linux服务器上面,同时运行多个不同版本的PHP程序,本文我们将使用FastCGI方式加载,并把过程详细记录下来方便大家参考. 常规的PHP配置方式有很多种,例如CGI.f ...

  8. 快速杀死占用8080端口进程的批处理(kill-8080.bat)

    @echo off setlocal enabledelayedexpansion for /f "delims= tokens=1" %%i in ('netstat -aon ...

  9. [Angular2 Form] Validation message for Reactive form

    <div class="form-field"> <label>Confirm Password: </label> <input typ ...

  10. hdu 4406 费用流

    这题问题就是当前时刻究竟选择哪门课程,易知选择是和分数有关的,而且是一个变化的权值,所以能够用拆点的方式,把从基础分到100分都拆成点.但若这样拆点的话,跑费用流时就必须保证顺序.这样就麻烦了..观察 ...