Html5 文件上传


文件上传时,要注意form的属性:enctype="multipart/form-data"

<form action="../event" method="post" enctype="multipart/form-data">

 


单文件上传:


<input type="file" name="eventImage" />

 多文件:


<input type="file" name="eventImage" multiple="true" />

 


 


 


JS控制文件数量和大小:


var files=document.getElementsByName('picfile').files, fs=files.length, s=0;
if(fs >10 ){
alert("上传的文件数量超过10个了!请重新选择!");
}else{
for(var i=0;i<fs;i++){
if(files[i].size > 1024*1024){
alert('"'+files[i].name + "这个文件大于1M!请重新选择!");
}
}
}

 

 

Html5 控制文件大小:

<input type="file" name="eventImage" multiple="true" size="80"/>

 


 


 


由于项目需要,有可能要Java控制文件大小,Spring控制文件大小:


java


public ResultVO createEvent(final MultipartHttpServletRequest request, final Long userId, final String lang,
final CreateEventRequestVO createEventRequestVO) {
final List<MultipartFile> fileList = request.getFiles("eventImage");
}

 Spring MVC

<!-- Upload -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- Set the maximum allowed size (in bytes) before uploads are refused. -->
<!-- -1 indicates no limit (the default). -->
<property name="maxUploadSize" value="307200" /> <!-- 300KB, 1024*300 -->

<property name="defaultEncoding" value="utf-8" />

<!-- Set the maximum allowed size (in bytes) before uploads are written to disk. -->
<!-- Uploaded files will still be received past this amount,but they will not be stored in memory. -->
<!-- Default is 10240,according to Commons File Upload. -->
<property name="maxInMemorySize" value="102400" /> <!-- 100KB, 1024*100 -->
</bean>

 

 

本站声明
本文转载自:http://youyu4.iteye.com/blog/2260643     作者:youyu4     发布日期:2015-11-30     本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。


 
© 2014-2016 ITdaan.com 粤ICP备14056181号