第一步:uploadify v3.2 在前台的配置
<input name="file" class="input-xlarge" type="file" id="fileupload">
<button id="upload_file_btn" class="btn btn-primary">提交</button>
js配置:
$("#fileupload").uploadify({
'auto':false,
'fileObjName' : 'filedata',
'swf': '../resources/exlib/uploadify/uploadify.swf',
'uploader': './file',
'fileTypeExts':'*.jpg;*.jpge;*.gif;*.png',
'buttonText': '选择图片',
'queueSizeLimit' : 1,
//选择上传文件后调用
'onSelect' : function(file) {
},
'onUploadStart' : function(file) {
},
'onCancel' : function(file) {
},
//上传到服务器,服务器返回相应信息到data里
'onUploadSuccess':function(file, data, response){
uploadFileSuccess(file, data, response);
},
'onUploadError':function(file, errorCode, errorMsg, errorString){
uploadFileError(file, errorCode, errorMsg, errorString);
}
});
function uploadFile()
{
var formdata =
{
"name":$("#picname").val(),
"link":$("#piclink").val(),
"title":$("#pictitle").val(),
"typeId":$("#pictype").val()
};
$('#fileupload').uploadify('settings','formData',formdata);//动态设置formData
//start to upload
$('#fileupload').uploadify('upload','*');
}
第二步. MVC后台:
刚开始想返回Map<String,String>,结果只有Chrome浏览器支持,IE9/FF都报了org.springframework.web.HttpMediaTypeNotAcceptableException错误,而且flash插件报I/O错误2038……只好返回String了。
@RequestMapping(value="file",method=RequestMethod.POST)
@ResponseBody
public String upload(HttpServletRequest request,final HttpServletResponse response)
{
final MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
final ImgInfoBean imgInfoBean = new ImgInfoBean();
final String name = (String) multipartRequest.getParameterValues("name")[0];
final String typeId = (String) multipartRequest.getParameterValues("typeId")[0];
final String link = (String) multipartRequest.getParameterValues("link")[0];
final String title = (String) multipartRequest.getParameterValues("title")[0];
imgInfoBean.setImgname(name);
imgInfoBean.setImgtypeid(Integer.parseInt(typeId));
imgInfoBean.setImgtitle(title);
imgInfoBean.setImglink(link);
final MultipartFile file = multipartRequest.getFile("filedata");
final boolean flag = imgInfoServiceImpl.addImg(file, imgInfoBean, request, response);
if(false == flag)
{
return new String("上传失败");
}
return new String("上传成功");
}
编码转换配置
<mvc:annotation-driven >
<!--确保@@ResponseBody 返回字符串是,格式是 text/plain;charset=UTF-8;
因为默认的是ISO-8859-1;-->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
自己的个人博客: bingtel-木犹如此的博客有兴趣可以关注下。
#########################