/***java代码**/
@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp","8192","UTF-8","10000"})
public Object add(@Param("..")ClothesType clothesType,int firstparent_id,@Param("Filedata")TempFile[] files,
int secondparent_id,ServletContext context,
HttpServletRequest req)
/***表单form*/
<form action="" id="addId" enctype="multipart/form-data">
<ul class="forminfo">
<li><label>分类名称:</label><input id="typeNane" name="clothes_name" type="text" class="dfinput" /><i>不能为空</i></li>
<li><label>上传图片</label><input id="imgName" name="Filedata" type="file"/></li>
</ul>
</form>
/***ajax提交*/
function save(){
var AjaxURL= "${base}/clothesType/add";
$.ajax({
type: "POST",
dataType: "json",
url: AjaxURL,
data: $('#addId').serialize(),
success: function (result) {
var strresult=result.result;
if(strresult==1){
alert("新增成功");
window.location.href="${base}/clothesType/search";
}
if(strresult==3){
alert("名称已经存在 ");
}
if(strresult==4){
alert("不能保存空记录");
}
},
error: function(data) {
alert("error:"+"保存失败");
}
});
}
/***异常**/
java.lang.IllegalArgumentException: Unknow Content-Type : application/x-www-form-urlencoded
6 回复
/**
解决方法:
上述方式,只能传递一般的参数,上传文件的文件流是无法被序列化并传递的。
不过如今主流浏览器都开始支持一个叫做FormData的对象,有了这个FormData,我们就可以轻松地使用Ajax方式进行文件上传了。
* */
var oData = new FormData(document.forms.namedItem("表单id" ));
oData.append( "CustomField", "This is some extra data" );
var oReq = new XMLHttpRequest();
oReq.open( "POST", "请求地址" , true );
oReq.onload = function(oEvent) {
if (oReq.status == 200) {
oOutput.innerHTML = "Uploaded!" ;
} else {
oOutput.innerHTML = "Error " + oReq.status + " occurred uploading your file.<br \/>";
}
};
oReq.send(oData);
@wendal 谢了兽总
添加回复
请先登陆