//var url = $(“#form”).attr(“action”);
// 1. 如果form表单不好处理,可以使用某个hidden隐藏域来设置请求地址
// <input type=”hidden” name=”action” value=”receive.jsp” />
var url = $(“input[name=’action’]”).val();
// 2. 也可以直接用某个dom对象的属性来获取
// <input id=”imageaction” type=”hidden” action=”receive.jsp”>
// var url = $(“#imageaction”).attr(“action”);
// 因为是string,所以服务器需要对数据进行转码,写文件操作等。
// 个人约定,所有http参数名字全部小写
console.log(dataurl);
//console.log(imagedata);
var data = {
imagename: “myImage.png”,
imagedata: imagedata
};
jQuery.ajax( {
url : url,
data : data,
type : “POST”,
// 期待的返回值类型
dataType: “json”,
complete : function(xhr,result) {
//console.log(xhr.responseText);
var $tip2 = $(“#tip2”);
if(!xhr){
$tip2.text(‘网络连接失败!’);
return false;
}
var text = xhr.responseText;
if(!text){
$tip2.text(‘网络错误!’);
return false;
}
var json = eval(“(“+text+”)”);
if(!json){
$tip2.text(‘解析错误!’);
return false;
} else {
$tip2.text(json.message);
}
//console.dir(json);
//console.log(xhr.responseText);
}
});
};
OK,搞定!你还需要做的,就是创建一个只管的用户界面,并允许你控制图片的大小。上传到服务器端的数据,并不需要处理enctype为 multi-part/form-data 的情况,仅仅一个简单的POST表单处理程序就可以了.
好了,下面附上完整的代码示例:
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>
<!DOCTYPE html>
<html>
<head>
<title>通过Canvas及File API缩放并上传图片</title>
<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
<meta http-equiv=”keywords” content=”Canvas,File,Image”>
<meta http-equiv=”description” content=”2013年8月8日,renfufei@qq.com”>
<script src=”http://code.jquery.com/jquery-1.7.1.min.js”></script>
<script>









