先上张效果图吧

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. shell学习四十天----awk的惊人表现

    awk的惊人表现 awk能够胜任差点儿全部的文本处理工作.     awk 调用 1.调用awk: 方式一:命令行方式 awk [-F field-separator ] 'commands' inp ...

  2. Codeforces Round #450 (Div. 2) D.Unusual Sequences (数学)

    题目链接: http://codeforces.com/contest/900/problem/D 题意: 给你 \(x\) 和 \(y\),让你求同时满足这两个条件的序列的个数: \(a_1, a_ ...

  3. Windows 7 下快速挂载和分离VHD文件的小脚本

    1.保存以下代码为VDM.vbs,放在Windows\system32下 Dim ArgsSet Args = WScript.ArgumentsTranArgs = " "For ...

  4. python3怎样画二维点图

    引用自:http://www.cnblogs.com/super-zhang-828/p/4792206.html import matplotlib.pyplot as pltplt.plot([1 ...

  5. JS数据类型的转换规则

    数据类型转换的规则 1 如果只有一个值,判断这个值是真还是假,遵循只有0,NaN,'',null,undefined这五个是假的,其余的都是真 2 如果是两个值比较是否相等,遵循以下规则: ![]-& ...

  6. pdf.js安装步骤和使用

    从github下载的源码不能直接使用,最好使用命令行下载安装 1.下载源码 git clone git://github.com/mozilla/pdf.js.git cd pdf.js 2.安装no ...

  7. 毕业两年做到测试经理的经历总结- 各个端的自动化,性能测试结合项目具体场景实战,分析客户反馈的Bug

    前言 最近看到行业的前辈都分享一些过往的经历来指导我们这些测试人员,我很尊敬我们的行业前辈,没有他们在前面铺路,如今我们这帮年轻的测试人估计还在碰壁或摸着石头过河,结合前辈们的经验,作为年轻的测试人也 ...

  8. 使用MongoDb连接数据库服务器

    链接MongoDb数据库服务器的字符串格式: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN] ...

  9. Nginx和Nginx+的比較(下)

    Nginx和Nginx+的比較(下) 作者:chszs.未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 内容紧接上一篇<Nginx和 ...

  10. context.getSystemService的简单说明

    在android开发过程中这种方法一定不会陌生,比方我们在获取WindowManager和LayoutInflater对象的时候就须要我们调用这种方法.这种方法在context这个抽象类的一个抽象方法 ...