canvas对象转换为Blob对象,并且ajax传入后台以MultipartFile接收

js 代码:

function upload(e) {
    var dataurl=e.toDataURL();
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    var blob= new Blob([u8arr], {type:mime});
    var fd = new FormData();
    fd.append("file", blob);
    fd.append("wareInfoId",334);
    $.ajax({
        type: "post",
        url: "/user/save/uploadUserCustomPic.do",
        async: true,
        data: fd,
        dataType:"json",
        processData: false,
        contentType: false,
        success: function(data) {
            if(data.result==1){
                alert(data.msg);

            }
        }
    });
}

后台Controller代码

@RequestMapping("save/uploadUserCustomPic")
public  void uploadUserCustomPic(MultipartFile file,Integer wareInfoId) throws IOException, DocumentException {

}

接下来就可以写业务代码了

 

你可能感兴趣的:(js,java)