[英]How to pass JavaScript value from View to Controller?
I have an ASP.NET MVC project and I´m using Cropbox.js : jQuery Image Crop Plugin - http://www.jqueryrain.com/demo/jquery-crop-image-plugin/ to crop an image of a user, but I cannot find how to get the cropped image to the controller. 我有一个ASP.NET MVC项目和I'm使用Cropbox.js:jQuery的图片裁剪插件- http://www.jqueryrain.com/demo/jquery-crop-image-plugin/裁剪用户的图像,但是我找不到如何将裁剪后的图像发送到控制器的方法。
JavaScript looks like this: JavaScript看起来像这样:
<script type="text/javascript">
window.onload = function () {
var options =
{
imageBox: '.imageBox',
thumbBox: '.thumbBox',
spinner: '.spinner',
imgSrc: 'avatar.png'
}
var cropper;
document.querySelector('#file').addEventListener('change', function () {
var reader = new FileReader();
reader.onload = function (e) {
options.imgSrc = e.target.result;
cropper = new cropbox(options);
}
reader.readAsDataURL(this.files[0]);
this.files = [];
})
document.querySelector('#btnCrop').addEventListener('click', function () {
var img = cropper.getAvatar()
document.querySelector('.cropped').innerHTML += '<img id="Portrait" src="' + img + '">';
})
document.querySelector('#btnZoomIn').addEventListener('click', function () {
cropper.zoomIn();
})
document.querySelector('#btnZoomOut').addEventListener('click', function () {
cropper.zoomOut();
})
};
</script>
I tried to use the following in the controller, but since I´m requesting the file, I´m not sure if it can even work: 我尝试在控制器中使用以下命令,但是由于我正在请求文件,因此不确定它是否可以正常工作:
HttpPostedFileBase file = Request.Files["Portrait"];
Maybe it would be possible to store the img file from javascript to the model? 也许可以将javascript中的img文件存储到模型中?
My friend has solved it by adding following: 我的朋友通过添加以下内容解决了该问题:
document.getElementById('avatarData').value = img;
To this part of the script: 对于脚本的这一部分:
document.querySelector('#btnCrop').addEventListener('click', function () {
var img = cropper.getAvatar()
document.querySelector('.cropped').innerHTML += '<img src="' + img + '">';
//new added
document.getElementById('avatarData').value = img;
})
Then used invisible input in View form: 然后在“视图”表单中使用不可见的输入:
<input type="hidden" id="avatarData" name="avatarData" value="">
Now I can catch it in controller: 现在我可以在控制器中捕获它:
var file = Request.Form["avatarData"];
And I´ll get: 我会得到:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQA..."
To work with this string, there is a very useful question & answer - MVC Convert Base64 String to Image, but ... System.FormatException 要使用此字符串,有一个非常有用的问题和答案-MVC将Base64字符串转换为Image,但是... System.FormatException
I don't know the jQuery Image Crop Plugin, but I think you'll need to do something like that: 我不知道jQuery Image Crop插件,但是我认为您需要执行以下操作:
Get the image's bytes, convert them to base64 and then send them to ViewController action using Ajax Post. 获取图像的字节,将其转换为base64,然后使用Ajax Post将其发送到ViewController操作。
i thing that you can use AjaxForm or HtmlForm and push it to any action. 我可以使用AjaxForm或HtmlForm并将其推送到任何操作。 Then use FormCollection
and watch your values. 然后使用FormCollection
并观察您的值。 For example in my Captcha generator I override some action for filter: 例如,在我的验证码生成器中,我为过滤器覆盖了一些操作:
public class CaptchaValidator : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
RegisterViewModel model = filterContext.ActionParameters["model"] as RegisterViewModel;
if (filterContext.HttpContext.Session["Captcha"] == null || filterContext.HttpContext.Session["Captcha"].ToString() != model.Captcha)
{
filterContext.ActionParameters["captchaValid"] = false;
}
else
{
filterContext.ActionParameters["captchaValid"] = true;
}
base.OnActionExecuting(filterContext);
}
}
and use in your controller: 并在您的控制器中使用:
[CaptchaValidator]
public async Task<ActionResult> Register(RegisterViewModel model, bool captchaValid)
I think that you can here change bool captchaValid
to your byte[]
. 我认为您可以在此处将bool captchaValid
更改为您的byte[]
。
I hope that it can help you :-) 我希望它可以帮助您:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.