简体   繁体   English

如何将JavaScript值从View传递到Controller?

[英]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.

相关问题 CodeIgniter - 如何将值从视图传递到控制器? - CodeIgniter - How to pass a value from a view to a controller? 如何将值从javascript传递到控制器函数? - How to pass value from javascript to controller function? 如何从控制器JavaScript传递数据以在AngularJS中查看JavaScript? - How to pass data from controller javascript to view javascript in AngularJS? 如何在视图中将变量从控制器传递到javascript? - How to pass a variable from controller to javascript within a view? 如何在 laravel 更改时使用 javascript 将变量从视图传递到 controller - How to pass a variable from view to controller using javascript on change in laravel 如何将整个模型从视图传递到控制器或Javascript - how to pass entire model from view to controller or Javascript 如何使用Jquery或Javascript将模型列表从控制器传递到视图 - How to pass Model List from Controller to View with Jquery or Javascript 如何将变量从控制器传递到不在视图内的javascript? - How to pass variable from controller to javascript that is not inside a view? 如何在JavaScript中从控制器的视图传递变量-Rails 4 - how to pass variable from view in controller in javascript - Rails 4 如何使用角度JS将javascript对象从视图传递到控制器? - How to pass the javascript object from view to controller using angular JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM