Nor*_*ode 5 javascript asp.net asp.net-mvc jquery
嗨,我想裁剪图像并将其上传到服务器上。
我正在使用croppie js插件,并使用get()方法通过使用WebImage类来获取将其裁剪在服务器上的点。
ASP.NET MVC代码
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ImageCrop(FormCollection fc)
{
WebImage data = WebImage.GetImageFromRequest();
if (data != null)
{
int x1, y1, x2, y2;
x1 = int.Parse(fc["x1"].ToString());
y1 = int.Parse(fc["y1"].ToString());
x2 = int.Parse(fc["x2"].ToString());
y2 = int.Parse(fc["y2"].ToString());
var fileName = Path.GetFileName(data.FileName);
fileName = Lawyer_id2 + ".jpeg";
var big = Server.MapPath("~/contents/ProfilePictures/big/" + fileName);
data.Crop(y1, x1, x2, y2);
data.Save(big);
}
}
Run Code Online (Sandbox Code Playgroud)
js代码
$uploadCrop = $('#upload-demo').croppie({
viewport: {
width: 200,
height: 200,
type: 'square'
},
boundary: {
width: 300,
height: 300
},
showZoomer: false,
mouseWheelZoom: false
});
readFile(fl);
$(".closeModal").on("click", function () {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'viewport'
}).then(function (resp) {
$('.upload-msg').css('display', '');
popupResult({
src: resp
});
});
var arr = $uploadCrop.croppie('get').points;
$("#x1").val(arr[0]);
$("#y1").val(arr[1]);
$("#x2").val(arr[2]);
$("#y2").val(arr[3]);
});
Run Code Online (Sandbox Code Playgroud)
我在隐藏的输入字段中获取所有点,然后将此点传递给webimge对象进行裁剪,但是问题是裁剪后的图像不能保持纵横比并错误地裁剪,浏览器端裁剪是完美的,但是当我将其传递给服务器端进行裁剪时它不像浏览器端那样工作,我不知道要解决这个问题。
Ary*_*ian 11
裁剪已经在客户端进行,您应该只将结果发送到服务器端。无需将裁剪点发送到服务器。
在 html 上定义Select和Upload按钮,以及一个 divid="main-cropper"
<div>
<div>
<div id="main-cropper"></div>
<a class="button actionSelect">
<input type="file" id="select" value="Choose Image" accept="image/*">
</a>
<button class="actionUpload">Upload</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在JS代码上,将croppie对象附加到潜水,定义视点边界,最后将请求发送到服务器以将结果存储为blob。在服务器端,假设将有一个Create控制器,其GetImage动作等待请求。testFileName.png被指定为文件名,您可以根据您的场景修改它。
var basic = $('#main-cropper').croppie({
viewport: { width: 200, height: 300 },
boundary: { width: 300, height: 400 },
showZoomer: false
});
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#main-cropper').croppie('bind', {
url: e.target.result
});
}
reader.readAsDataURL(input.files[0]);
}
}
$('.actionSelect input').on('change', function () { readFile(this); });
$('.actionUpload').on('click', function() {
basic.croppie('result','blob').then(function(blob) {
var formData = new FormData();
formData.append('filename', 'testFileName.png');
formData.append('blob', blob);
var MyAppUrlSettings = {
MyUsefulUrl: '@Url.Action("GetImage","Create")'
}
var request = new XMLHttpRequest();
request.open('POST', MyAppUrlSettings.MyUsefulUrl);
request.send(formData);
});
});
Run Code Online (Sandbox Code Playgroud)
在服务器上,在Create控制器中:
[HttpPost]
public ActionResult GetImage(string filename, HttpPostedFileBase blob)
{
var fullPath = "~/Images/" + filename;
blob.SaveAs(Server.MapPath(fullPath));
return Json("ok");
}
Run Code Online (Sandbox Code Playgroud)
生成 uuid 字符串,并将其设置为文件名并存储在数据库中也是有意义的。