Bootstrap的fileinput插件实现多文件上传的方法

*1.bootstrap-fileinput 插件git下载地址

https://github.com/kartik-v/bootstrap-fileinput.git

2.解决使用bootstrap-fileinput得到返回值

上传图片

?
1
2
3
4
5
6
7
8
9
10
11
12
$( "#file-0a" ).fileinput({
uploadUrl : "/upload_img" , //上传图片的url
allowedFileExtensions : [ 'jpg' , 'png' , 'gif' ],
overwriteInitial : false ,
maxFileSize : 1000, //上传文件最大的尺寸
maxFilesNum : 1, //上传最大的文件数量
initialCaption: "请上传商家logo" , //文本框初始话value
//allowedFileTypes: ['image', 'video', 'flash'],
slugCallback : function (filename) {
return filename.replace( '(' , '_' ).replace( ']' , '_' );
}
});

注意上传图片事件完之后,得到返回值写法

?
1
2
3
4
5
6
$( '#file-0a' ).on( 'fileuploaded' , function (event, data, previewId, index) {
var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
console.log(response); //打印出返回的json
console.log(response.paths); //打印出路径
});

jsp页面

?
1
2
<input id= "file-0a" class= "file" type= "file" multiple
data-min-file-count= "1" name= "upload_logo" >

其中data-min-file-count=”1”是指文件上传最低数量

3.服务端代码

采用了spring自带插件上传,框架为Springmvc

Bean

?
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.List;
public class Picture {
private List<String> paths;
public List<String> getPaths()
{
return paths;
}
public void setPaths(List<String> paths)
{
this .paths = paths;
}
}

Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@ResponseBody
@RequestMapping(value= "upload_img" ,method=RequestMethod.POST)
public Picture uploadImage(@RequestParam MultipartFile[] upload_logo) throws IOException{
log.info( "上传图片" );
Picture pic = new Picture();
List<String> paths = new ArrayList<String>();
String dir = UploadUtil.getFolder();
for (MultipartFile myfile : upload_logo){
if (myfile.isEmpty()){
log.info( "文件未上传" );
} else {
log.info( "文件长度: " + myfile.getSize());
log.info( "文件类型: " + myfile.getContentType());
log.info( "文件名称: " + myfile.getName());
log.info( "文件原名: " + myfile.getOriginalFilename());
log.info( "========================================" );
//上传文件 返回路径
String path = UploadUtil.writeFile(myfile.getOriginalFilename(), dir, myfile.getInputStream());
log.info( "文件路径:" +path);
paths.add(path);
}
}
pic.setPaths(paths);
return pic;
}

uploadUtil

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
private static final Logger log = LoggerFactory.getLogger(UploadUtil.class);
private UploadUtil() {
}
private static SimpleDateFormat fullSdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS" );
private static SimpleDateFormat folder = new SimpleDateFormat(
"yyyy" + File.separator + "MM" + File.separator + "dd" );
/**
* 返回yyyy File.separator MM File.separator dd格式的字符串
*
* @return
*/
public static String getFolder() {
return folder.format( new Date());
}
/**
* 文件上传
*
* @param srcName
* 原文件名
* @param dirName
* 目录名
* @param input
* 要保存的输入流
* @return 返回要保存到数据库中的路径
*/
public static String writeFile(String srcName, String dirName, InputStream input) throws IOException {
log.info(srcName);
// 取出上传的目录,此目录是tomcat的server.xml中配置的虚拟目录
String uploadDir = ContextUtil.getSysProp( "upload_dir" ); //设置你上传路径
// 取出虚拟目录的访问路径
String virtualDir = ContextUtil.getSysProp( "virtual_dir" ); //设置你虚拟目录访问路径
// 重命名文件
if ( null != srcName) {
srcName = srcName.substring(srcName.indexOf( "." ));
} else {
srcName = ".jpg" ;
}
String filename = "" ;
// 得到要上传的文件路径
filename = uploadDir + File.separator + dirName + File.separator + fullSdf.format( new Date()) + srcName;
// 得到将要保存到数据中的路径
String savePath = filename.replace(uploadDir, "" );
savePath = virtualDir + savePath.replace( "\\" , "/" );
File file = new File(filename);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
// 一次30kb
byte[] readBuff = new byte[1024 * 30];
int count = -1;
while ((count = input.read(readBuff, 0, readBuff.length)) != -1) {
fos.write(readBuff, 0, count);
}
fos.flush();
fos.close();
input.close();
return savePath;
}

4.解决上传时候遇到的一些问题

如遇见点击上传之后,进度条显示为100%,jsp页面显示[Object,obejct],那么注意你后台返回的是否为json对象。 

以上所述是小编给大家介绍的Bootstrap的fileinput插件实现多文件上传的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

原文链接:http://blog.csdn.net/qq_23254453/article/details/51199153
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值