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命令进入文件夹,将压缩包 ...
随机推荐
- 微信小程序——使用vue构建小程序【外传】
文档 http://mpvue.com/mpvue/ 根据文档构建完成的页面如下 更多的,还要继续看下文档~
- 微信小程序——demo合集及简单的文档解读【五】
官方Demo https://github.com/wechat-miniprogram/miniprogram-demo 其他Demo https://www.cnblogs.com/ytkah/p ...
- Windows如何使用bin文件下的命令
开发人员安装了一些软件,例如git.maven.gradle等,需要用到对应的bin文件夹下的相应的命令. 如果直接使用,会报错“不是内部或外部命令,也不是可运行的程序或批处理文件” 一.这时往往会配 ...
- Install Nagios (Agent) nrpe client and plugins in Ubuntu/Debian
安装apt-get install nagios-nrpe-server nagios-plugins 修改nrpe.cfgvi /etc/nagios/nrpe.cfg修改Allow Host,添加 ...
- 【CF526G】Spiders Evil Plan(贪心)
[CF526G]Spiders Evil Plan(贪心) 题面 洛谷 CodeForces 给定一棵树,要求选择\(y\)条链,满足被链覆盖的所有点在树上联通,且\(x\)必定在联通块中. 对于每次 ...
- CSS居中的几种方式总结
1.水平居中的 margin:0 auto; 这个是用于子元素上的,前提是不受float影响 <style type="text/css"> .box{ width: ...
- Nginx+Tomcat-cluster构建
-----------ReProxy-------------------------Client-----------192.168.56.202 nginx 192.168.56.200 Tomc ...
- python 获取当前文件夹下所有文件名
os 模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os 4 5 def file_name(file_d ...
- uvaLive7303 Aquarium (kruskal)
题意:给R*C的房间,每个房间被左上-右下或右上-左下的墙分割为两个小房间,将分割移除有一定花费,问使所有小房间联通需要的最小花费 把每个房间分成左右(上下?)两个点,判一判,本来就联通的加零边,一个 ...
- SSH防暴力破解脚本
crontab -e 编辑添加一下内容 1 1 * * * sh /root/bin/Denyhosts.sh 脚本内容 #!/bin/bash #Denyhosts SHELL SCRIPT #20 ...