前台将base64 转为二进制

调用以下方法

function dataURLtoBlob(dataurl) {
    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);
    }
    return new Blob([u8arr], {type: mime});
}

ajax

var fileURL = dataURLtoBlob(img);
var file = new FormData();
file.append("file", fileURL);
$.ajax({
    url: "${pageContext.request.contextPath}/upload/uploadImg",
    type: 'POST',
    data: file,
    processData: false,
    contentType: false,
    timeout: 10000,
    dataType: "json",
    success: function (data) {
        console.log(data.id);
        uploadName(data.id, name);
    },
    error: function () {
        $(".mask").hide();
        alert("图片合成失败!");
    }
});

后台controller

@PostMapping(value = "uploadImg")
@ResponseBody
 public String uploadImg(@RequestParam CommonsMultipartFile file) throws IOException {
     Img img = imgService.saveImg(file, did);
     JSONObject response = new JSONObject();
     response.put("id", img.getId());
     return response.toString();

 }

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