最近做了个简易的基于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 文件拖拽上传、选择上传的更多相关文章

  1. html5 文件拖拽上传

    本文首先发表在  码蜂笔记 : http://coderbee.net/index.php/web/20130703/266 html5 文件拖拽上传是个老话题了,网上有很多例子,我一开始的代码也是网 ...

  2. php和js实现文件拖拽上传

    Dropzone.js实现文件拖拽上传 http://www.sucaihuo.com/php/1399.html demo http://www.sucaihuo.com/jquery/13/139 ...

  3. day25—JavaScript实现文件拖拽上传案例实践

    转行学开发,代码100天——2018-04-10 今天记录一个利用JavaScript实现文件拖拽上传到浏览器,后天将文件打开的小案例. 基本功能:1点击添加文件 2 文件拖拽添加 html: < ...

  4. Web存储及文件拖拽

    存储 实现内容的永久保存(localStorage) 保存: localStorage.自定义键名="123"; 获取: //判断是否有内容 if(localStorage.自定义 ...

  5. VC实现文件拖拽OnDropFiles

    文章转自http://blog.csdn.net/zamaolangzi/article/details/5645284 使用过QQ的人都知道,只要把文件拖拽到消息框中就可以传送文件了.那么这种功能是 ...

  6. html5 drag 文件拖拽浅淡

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. jQuery可拖拽3D万花筒旋转特效

    这是一个使用了CSS3立体效果的强大特效,本特效使用jQuery跟CSS3 transform来实现在用户鼠标按下拖动时,环形图片墙可以跟随鼠标进行3D旋转动画. 效果体验:http://hovert ...

  8. C#之winform实现文件拖拽功能

    将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter ...

  9. Linux下安装VMware Tools(使虚拟机支持文件拖拽)

    如图点击虚拟机找到安装VMware Tools选项,点击后会在虚拟机桌面显示一个光盘,双击进入如下页面: 选择压缩包将其复制放入Home中不带中文的文件夹: 打开终端,输入cd命令进入文件夹,将压缩包 ...

随机推荐

  1. 【hjmmm网络流24题补全计划】

    本文食用方式 按ABC--分层叙述思路 可以看完一步有思路后自行思考 飞行员配对问题 题目链接 这可能是24题里最水的一道吧... 很显然分成两个集合 左外籍飞行员 右皇家飞行员 跑二分图最大匹配 输 ...

  2. SHELL 脚本小技巧

    脚本很简单,直接上功能介绍及脚本,可以做模板使用: 记录日志,记录脚本开始执行时间.结束时间 usage 函数,脚本需接参数执行,避免误执行,告诉用户,这个脚本的使用方法 加锁,创建锁文件,脚本不允许 ...

  3. [HNOI2015]落忆枫音 解题报告

    [HNOI2015]落忆枫音 设每个点入度是\(d_i\),如果不加边,答案是 \[ \prod_{i=2}^nd_i \] 意思是我们给每个点选一个父亲 然后我们加了一条边,最后如果还这么统计,那么 ...

  4. bandwagon host

    104.20.6.63 bandwagonhost.com 104.20.6.63 bwh1.net

  5. [模板] Manacher(马拉车)算法

    用途 求回文子串 做法 先考虑回文子串以某字符为中心的情况,即长度为奇数 推着做,记rad[i]为以i位置为中心的最大半径(包含中点) 考虑怎么求rad[i].找之前的一个右端点最靠右的位置p,设它的 ...

  6. 算法练习:求字符串的最长重复子串(Java实现)

    1. 求字符串的最长重复子串 例如:aaaaaaaaabbbbcccaaassscvvv这里面的最长重复子串为aaaaaaaaa 算法思路:算法时间复杂度(O(n)) 1. 将这一个字符串先转成cha ...

  7. hibernate 5的二级缓存案例讲解

    hibernate 5的二级缓存案例讲解 本帖最后由 鱼丸儿 于 2018-1-20 11:44 编辑 大家好,今天来记录讲解一下磕磕绊绊的hibernate5 的二级缓存配置,一条路摸到黑 那么在这 ...

  8. A1127. ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  9. JavaScrip相关知识总结

    1.javascript是一种基于对象的语言,其中有四个常用的“全局对象”的成员使用,因为没有“全局对象关键字global”而直接使用,所以感觉像违背了JavaScript基于对象编程的原则,但其实是 ...

  10. 为Druid监控配置访问权限(配置访问监控信息的用户与密码)

    转: l 为Druid监控配置访问权限(配置访问监控信息的用户与密码) 2014-09-26 09:21:48         来源:renfufei的专栏   收藏   我要投稿   Druid是一 ...