苦苦挣扎于Ajax Post Image

时间:2017-01-15 23:06:14

标签: javascript java ajax

我一直在努力将图像数据发布到Java服务器上。我得到400(每次都有错误请求)。

我尝试过的一些事情

var imgData = renderer.domElement.toDataURL();
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 400;
canvas.height = 400;

ctx.drawImage(renderer.domElement,- (renderer.domElement.width/2 - canvas.width/2),- (renderer.domElement.height/2 - canvas.height/2), renderer.domElement.width, renderer.domElement.height);
var resizedImageData = canvas.toDataURL('image/png');
$("#sceneThumbImage").attr("src",resizedImageData);


var ajax = new XMLHttpRequest();
var url = '/scene/'+currentScene.id+'/thumb';

ajax.open("POST",url,false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(resizedImageData);

$.post('/scene/'+currentScene.id+'/thumb',resizedImageData,function(){console.log('success')},function(e){console.log('failed',e)});

Java方法

@ResponseBody
    @RequestMapping(value = "/scene/{sceneId}/thumb", method = RequestMethod.POST)
    public ResponseEntity<String> saveSceneThumb(
            @PathVariable("sceneId") String sceneId,
            @RequestParam("thumbName") String thumbName,
            DefaultMultipartHttpServletRequest request) {

        // If not logged in, return early
        if (!isLoggedIn(request))
            return getErrorResponse(1234, "not logged in");

        try {
            // Create and populate the thumb with the new bitmap
            //File file = new File(Constants.TEMP_DIR_PATH + thumbName);
            //request.getFile("thumb").transferTo(file);

            java.io.File file = new java.io.File(Constants.TEMP_DIR_PATH
                    + thumbName);
            request.getFile("thumb").transferTo(file);

            // upload thumb to the scenes bucket
            s3UploadService.uploadFile(assetsBucket, file);

            return getSuccessResponse();
        } catch (Exception e) {
            return getErrorResponse(1234, "Error uploading scene thumb");
        }
    }   }   }

每次都是错误的请求。如果有人知道一个简单的方法来调试这个或者发现我做错了什么,请告诉我!

0 个答案:

没有答案