web 平台实现图片shangchuan

之前有写过文件上传,其实这两个实质是差不多的,只是文件上传需要解析一下文件。具体代码如下:

jsp:

             <div class="form-group">
<label for="a_autoSync" class="col-sm-4 control-label">属性图标<font     color="red">*</font></label>
<div class="col-sm-6">
<input id="add_property_img" name="add_property_img" type="file">
</div>

 </div>


js:

function addpropertyConfirm(){

得到文本框的value,再进行编码转换 可以解决浏览器兼容问题:

priority = encodeURI(encodeURI(priority));
$.ajaxFileUpload({
url: 
secureuri : false,
fileElementId : 'add_property_img',// file标签的id
dataType : 'text',// 返回数据的类型
type : 'post',
success : function(data, status) {
var json = eval("("+data+")");
            if (!json.result) {
alertErrorAtBottomRight(json.errorMsg);
} else {
alertSuccessAtBottomRight("添加成功");
resetForm('add_property_form');
$("#add_property").modal('hide');
$("#productPropertyList").DataTable().draw();
}
},
error : function(data, status, e) {
alert(e);
}

});

}


control:

@RequestMapping(value = "")
public @ResponseBody AjaxProcessResult add(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value = "file标签的id") MultipartFile add_property_img){

 

        try {

           进行解码:

        priority = URLDecoder.decode(priority, "UTF-8");
} catch (Exception e) {
LOG.error(e);
}
        
if(LOG.isInfoEnabled()){
LOG.info("");
LOG.info("priority:"+priority);
}
if(StringHandler.isEmpty(priority)){
return AjaxProcessResult.getErrorResult("优先级不能为空");
}

try {

                  //得到文件名

                 String fileName = add_property_img.getOriginalFilename();

//上传地址

String property_img=上传地址的工具类.upload("..../"+fileName, add_property_img.getInputStream());
//调用方法
int count =productPropertyService.addProductProperty();
if(count > 0){
return AjaxProcessResult.getSuccessResult();
}else{
return AjaxProcessResult.getErrorResult("添加失败");
}
} catch (Exception e) {
if(LOG.isErrorEnabled())
LOG.error(e.getMessage());
return AjaxProcessResult.getErrorResult(e.getMessage());
}
}


OK,图片上传完成



猜你喜欢

转载自blog.csdn.net/l_blackeagle/article/details/80014807