上传使用cropper.js插件裁剪的图像
我在我的应用程序中使用了 cropper.js 插件来裁剪图像。我能够裁剪图像。现在我正在尝试上传图片而不是下载它们。
我更新了显示裁剪图像的模态窗口,如下所示:
I have used cropper.js plugin in my application to crop images. I am able to crop the images. Now I am trying to upload the images instead of downloading them. I have updated the the modal window that shows the cropped image as follows:
<!-- Show the cropped image in modal -->
<div class="modal fade docs-cropped" id="getCroppedCanvasModal" aria-hidden="true" aria-labelledby="getCroppedCanvasTitle" role="dialog" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="getCroppedCanvasTitle">Cropped</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" style = "float: left;">Close</button>
<form class="form-inline" id="croperImgForm" role="form" method="post" action="${rc.getContextPath()}/module/simplewebcontent/save-image" enctype="multipart/form-data">
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox1" value="option1"> Save Image
</label>
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox2" value="option2">Save Thumbnail
</label>
<button type="submit" class="btn btn-primary" id="svImg" style = "margin-left: 10px;">Save</button>
</form>
</div>
</div>
</div>
</div><!-- /.modal -->
这里我在底部添加了一个表单,其中包含保存图像的选项。我正在尝试使用以下脚本来保存表单:
Here I have added a form in the bottom with option to Save the image. I am trying to use the following script to save the form:
$("#svImg").click( function()
{
alert('button clicked');
$("#croperImgForm").submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
dataType : "html",
success:function(htmlData)
{
},
error: function( xhr, status, errorThrown ) {
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
});
e.preventDefault();
e.unbind();
});
});
现在我从文档中发现我应该使用以下代码来保存图像:
Now I found from the docs that I should use the following code to save the image:
// Upload cropped image to server if the browser supports `canvas.toBlob`
$().cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
formData.append('croppedImage', blob);
$.ajax('/path/to/upload', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function () {
console.log('Upload success');
},
error: function () {
console.log('Upload error');
}
});
});
但我不知道如何使用此代码作为我正在尝试的表单提交的一部分实现ajax调用。
But I am not sure how to use this code as a part of the form submission which I am trying to implement as an ajax call.
我也使用Spring mvc,因此在上传图像时控制器方法中的参数是什么。
Also I use Spring mvc, so what would be the parameters in the controller method in case of this image upload.
虽然很久以前就问过这个问题,但我仍然在分享一个链接到我刚刚在其他人的背景下发布的解决方案问题。
Though this question was asked long back, still I am sharing a link to the solution I just posted in context to someone else's problem.
我希望它可以帮助在此页面上找到的人。 (就像我登陆并找不到解决方案一样)
使用Cropper.js裁剪图像,然后上传(或显示)裁剪后的图像。
I hope it will help someone found on this page. ( Like I landed and found no solution ) Crop image using Cropper.js, then upload (or display) the cropped image.