SpringMvc+jQuery 文件拖拽上传、选择上传
最近做了个简易的基于boostrap的文件上传功能,jsp版本的,后续会完善更多的功能,不过现在已经能用了,需要的小伙伴,直接引用下面的文件内容直接copy到自己的项目中就ok了,效果如图:

fileupload.css:
.fileupload_box {
position:relative;
width: 100%;
height: 100%;
border: 3px dashed #E5E5E5;
text-align: center;
z-index: 2000;
cursor: pointer;
margin:0 auto;
}
.fileupload_message {
position: absolute;
font-size: 15px;
color: #999;
font-weight: normal;
height:30px;
top:20px;
width: 100%;
}
.odao_browser{
position:absolute;
width:50%;
height:36px;
line-height:36px;
left:25%;
bottom:10px;
-moz-border-radius: 3px; /* Firefox */
-webkit-border-radius: 3px; /* Safari 和 Chrome */
border-radius:3px; /* Opera 10.5+, 以及使用了IE-CSS3的IE浏览器 */
background:#5c6bc0;
color:white;
cursor: pointer;
}
.odao_browser input {
position:absolute;
top:0;
width:100%;
height:100%;
opacity: .0;
filter: alpha(opacity= 0);
direction: ltr;
cursor: pointer;
}
odao_fileupload_plugin.js:
(function($) {
$.fn.odao_fileupload=function(setting){
var defaults = {
url: document.URL,
method: 'POST',
extraData: {},
maxFileSize: 0,
maxFiles: 0,
allowedTypes: '*',
extFilter: null,
dataType: null,
fileName: 'file',
onUploadSuccess: function(fileName,url){}
};
setting = $.extend({},defaults,setting);
var fn = {
init:function(){
var _this = this;
//拖拽文件上传
var $targetBox = document.getElementById($(_this).attr('id'));
$targetBox.addEventListener("drop", function(e) {
//取消默认浏览器拖拽效果
e.preventDefault();
var fileList = e.dataTransfer.files;
fn.upload.call(_this,fileList[0]);
}, false);
//选择文件上传
$(_this).find('input').on('change',function(e){
var file = e.target.files;
var file = file.item(0);
fn.upload.call(_this,file);
});
},upload:function(file){
if(fn.checkType(file)) return;
var _this = this;
var fd = new FormData();
fd.append('files', file);
$.ajax({
url: $ctx+"/fileUpload/xxxxx.do",
type: 'POST',
success: function(data){
if(data.success)
setting.onUploadSuccess.call(_this,file.name,data);
}
},
error: function(){
showReturnMsg('上传失败','error');
},
data: fd,
cache: false,
contentType: false,
processData: false
});
},
//允许上传的文件类型
checkType:function(file){
if (setting.allowedTypes != '*' && setting.allowedTypes.indexOf(file.type) == -1) {
showReturnMsg('文件格式错误,只能上传'+setting.allowedTypes+'格式的文件','error');
return true;
}
return false
}
};
return this.each(function(i,n){
fn.init.call($(n));
});
};
$(document).on('dragenter', function (e) { e.stopPropagation(); e.preventDefault(); });
$(document).on('dragover', function (e) { e.stopPropagation(); e.preventDefault(); });
$(document).on('drop', function (e) { e.stopPropagation(); e.preventDefault(); });
})(jQuery);
jsp:
<div id="target_box" class="fileupload_box">
<p class="fileupload_message" >将图片拖动到这里</p>
<p class="odao_chooseFileUpload odao_browser">选择文件
<input type="file" name="files" multiple="multiple">
</p>
</div>
调用方法:
$('#target_box').odao_fileupload({
onUploadSuccess:function(fileName,attachmentUrl){
//保存上传文件信息并记录日志
$.ajax({
type: "post",
url: '',
data: {''},
dataType: "json",
success: function(data){
if(data.success){
alert('上传成功');
}else{
alert('上传失败');
}
}
});
},
allowedTypes:'image/png'
});
spring 配置:
<!-- 文件上传相关 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--one of the properties available;the maximum upload size in bytes 100M-->
<property name="maxUploadSize" value="104857600"/>
</bean>
FileUpload.java:
@RequestMapping("/uploadProjectBgPicture.do")
public String uploadProjectBgPicture(@RequestParam MultipartFile[] files,HttpServletRequest request,HttpServletResponse response) throws IOException{
具体操作内容
}
SpringMvc+jQuery 文件拖拽上传、选择上传的更多相关文章
- html5 文件拖拽上传
本文首先发表在 码蜂笔记 : http://coderbee.net/index.php/web/20130703/266 html5 文件拖拽上传是个老话题了,网上有很多例子,我一开始的代码也是网 ...
- php和js实现文件拖拽上传
Dropzone.js实现文件拖拽上传 http://www.sucaihuo.com/php/1399.html demo http://www.sucaihuo.com/jquery/13/139 ...
- day25—JavaScript实现文件拖拽上传案例实践
转行学开发,代码100天——2018-04-10 今天记录一个利用JavaScript实现文件拖拽上传到浏览器,后天将文件打开的小案例. 基本功能:1点击添加文件 2 文件拖拽添加 html: < ...
- Web存储及文件拖拽
存储 实现内容的永久保存(localStorage) 保存: localStorage.自定义键名="123"; 获取: //判断是否有内容 if(localStorage.自定义 ...
- VC实现文件拖拽OnDropFiles
文章转自http://blog.csdn.net/zamaolangzi/article/details/5645284 使用过QQ的人都知道,只要把文件拖拽到消息框中就可以传送文件了.那么这种功能是 ...
- html5 drag 文件拖拽浅淡
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery可拖拽3D万花筒旋转特效
这是一个使用了CSS3立体效果的强大特效,本特效使用jQuery跟CSS3 transform来实现在用户鼠标按下拖动时,环形图片墙可以跟随鼠标进行3D旋转动画. 效果体验:http://hovert ...
- C#之winform实现文件拖拽功能
将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter ...
- Linux下安装VMware Tools(使虚拟机支持文件拖拽)
如图点击虚拟机找到安装VMware Tools选项,点击后会在虚拟机桌面显示一个光盘,双击进入如下页面: 选择压缩包将其复制放入Home中不带中文的文件夹: 打开终端,输入cd命令进入文件夹,将压缩包 ...
随机推荐
- python3,打印一年的某一天是一年的第几天
year = int(input('year:')) month = int(input('month:')) day = int(input('day:')) months = (0,31,59,9 ...
- Tmutarakan Exams URAL - 1091(莫比乌斯函数 || 容斥)
题意: 求1 - s 中 找出k个数 使它们的gcd > 1 求这样的k个数的对数 解析: 从每个素数的倍数中取k个数 求方案数 然后素数组合,容斥一下重的 奇加偶减 莫比乌斯函数的直接套模 ...
- quartus prime 16.0 报警告 inferring latch
前言 当写always组合逻辑块时,可能会写出 poor code.综合时软件会推断出锁存器.例如下面代码: always @* begin 'b1) begin w = (a & b) ^ ...
- 前后端分离之vue2.0+webpack2 实战项目 -- html模板拼接
对于前后端分离,如何把一个页面的公共部分比如head, header, footer, content等组合成一个完整的html 是一个值得考虑的地方. 对于php,我们可以利用include加载其他 ...
- thusc2017
巧克力 题目描述 "人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道." 明明收到了一大块巧克力,里面有若干小块,排成
- [HEOI2016/TJOI2016]游戏 解题报告
[HEOI2016/TJOI2016]游戏 看起来就是个二分图匹配啊 最大化匹配是在最大化边数,那么一条边就代表选中一个坐标内的点 但是每一行不一定只会有一个匹配 于是把点拆开,按照'#'划分一下就好 ...
- cf1000F One Occurrence (线段树)
这题我是离线做的 设i位置的数上次出现的位置是pre[i](如果第一次出现那就是0) 可以想到,用线段树维护一个区间的pre的最小值,如果它小于区间左端点,那这个数就是一个合法的答案 但直接这样做是错 ...
- LOJ#6278. 数列分块入门 2
在一个区间上进行操作,一种操作是某个小区间都加上c,另一个查找这个区间内大于c*c的数 我们可以另外开一个数组在保存a中的每个分块内的相对值,然后每次对a加值,并把a的值赋给b,不同的是b内的各个分块 ...
- bzoj4337树的同构
树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如果能够把树T1的所有点重 ...
- FZU 2150 Fire Game (bfs+dfs)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...