如何发送JavaScript文件对象+数据到MVC 6控制器 - ASP.NET 5 [英] How to send Javascript File Object+ Data to MVC 6 Controller - ASP.NET 5

查看:140
本文介绍了如何发送JavaScript文件对象+数据到MVC 6控制器 - ASP.NET 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是阿贾克斯,我知道在文件和属性的一个函数里。
什么也没发生,我拖着我的文件,并删除它,但没有与阿贾克斯发生。当我输出属性,我看到我的JS函数的文件,但它并没有将其发送到控制器的页面。我不认为这是正常工作,当我调试我的指数控制器存在在我的参数没有任何文件的属性。阿贾克斯是不是给我的成功或失败的警报消息。

我的最终目标是让上传所以文件中的数据,那么我可以分析它。

的JavaScript

 函数SENDFILE(文件){
        $阿贾克斯({
            键入:POST,
            网址:HomeController中/索引,//我们调用的方法
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据:{文件名:file.name,文件类型:file.type,作品尺寸:file.size},
            数据类型:JSON
            成功:函数(结果){
                警报('!耶它的工作!');
                //或者,如果你正在返回的东西
                警报(我回来......'+ result.WhateverIsReturning);
            },
            错误:功能(结果){
                警报('哦,不:(');
            }
        });        输出(
            < P>档案数据:其中;强>中+ file.name +
            < / STRONG>类型:其中;强>中+ file.type +
            < / STRONG>大小:LT;强>中+ file.size +
            < / STRONG>字节< / P>中
        );    }

AJAX

  $阿贾克斯({
            键入:POST,
            网址:HomeController中/索引,//我们调用的方法
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据:{文件名:file.name,文件类型:file.type,作品尺寸:file.size},
            数据类型:JSON
            成功:函数(结果){
                警报('!耶它的工作!');
                //或者,如果你正在返回的东西
                警报(我回来......'+ result.WhateverIsReturning);
            },
            错误:功能(结果){
                警报('哦,不:(');
            }
        });

C#

 公共IActionResult指数(字符串文件名,字符串的fileType,INT档案大小)
    {
        ViewBag.Environments = _configHelper.GetEnvironments();
        VAR模型=新HomeViewModel {环境=DEV};
        返回查看(模型);
    }

CSHTML

 < D​​IV CLASS =表单组COL-MD-12>
            <标签>公司标识:< /标签>
            < D​​IV ID =filedrag>< TextArea类=表格控行=5ID =textAreaCompanyIDs占位符=文件拖放这里>< / textarea的>
            < / DIV>
        < / DIV>


解决方案

要获得在控制器中的文件,你必须发送完整的文件。

尝试是这样的:

AJAX

  $阿贾克斯({
            键入:POST,
            网址:HomeController中/索引,//我们调用的方法
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据:{文件:文件,文件类型:file.type,作品尺寸:file.size},
            数据类型:JSON
            成功:函数(结果){
                警报('!耶它的工作!');
                //或者,如果你正在返回的东西
                警报(我回来......'+ result.WhateverIsReturning);
            },
            错误:功能(结果){
                警报('哦,不:(');
            }
        });

控制器

 公共IActionResult指数(HttpPostedFileBase文件,字符串的fileType,INT档案大小)
    {
        ViewBag.Environments = _configHelper.GetEnvironments();
        VAR模型=新HomeViewModel {环境=DEV};
        返回查看(模型);
    }

This Ajax is inside a function that I know has the file and properties in. Nothing is happening, I am dragging my file and dropping it but nothing is happening with the Ajax. When I output the properties to the page I see that my function in JS has the file but it doesn't send it to the controller. I do not think it is working correctly, and when I debug my Index Controller there is no file properties in my parameters. The Ajax isn't giving me success or failure alert message.

My End goal is to get the data inside the file uploaded so I can then parse it.

JavaScript

        function sendFile(file) {


        $.ajax({
            type: "POST",
            url: "HomeController/Index", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: { filename: file.name, fileType: file.type, fileSize: file.size },
            dataType: "json",
            success: function(result) {
                alert('Yay! It worked!');
                // Or if you are returning something
                alert('I returned... ' + result.WhateverIsReturning);
            },
            error: function(result) {
                alert('Oh no :(');
            }
        });

        Output(
            "<p>File information: <strong>" + file.name +
            "</strong> type: <strong>" + file.type +
            "</strong> size: <strong>" + file.size +
            "</strong> bytes</p>"
        );

    }

AJAX

            $.ajax({
            type: "POST",
            url: "HomeController/Index", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: { filename: file.name, fileType: file.type, fileSize: file.size },
            dataType: "json",
            success: function(result) {
                alert('Yay! It worked!');
                // Or if you are returning something
                alert('I returned... ' + result.WhateverIsReturning);
            },
            error: function(result) {
                alert('Oh no :(');
            }
        });

C#

        public IActionResult Index(string fileName, string fileType, int fileSize)
    {
        ViewBag.Environments = _configHelper.GetEnvironments();
        var model = new HomeViewModel { Environment = "DEV" };
        return View(model);
    }

CSHTML

            <div class="form-group col-md-12">
            <label>Company Ids:</label>
            <div id="filedrag"><textarea class="form-control" rows="5" id="textAreaCompanyIDs" placeholder="Drop Files Here"></textarea>
            </div>
        </div>

解决方案

To get the file in the controller, you have to send the full file.

Try something like:

AJAX:

 $.ajax({
            type: "POST",
            url: "HomeController/Index", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: { file: file, fileType: file.type, fileSize: file.size },
            dataType: "json",
            success: function(result) {
                alert('Yay! It worked!');
                // Or if you are returning something
                alert('I returned... ' + result.WhateverIsReturning);
            },
            error: function(result) {
                alert('Oh no :(');
            }
        });

Controller:

   public IActionResult Index(HttpPostedFileBase file, string fileType, int fileSize)
    {
        ViewBag.Environments = _configHelper.GetEnvironments();
        var model = new HomeViewModel { Environment = "DEV" };
        return View(model);
    }

这篇关于如何发送JavaScript文件对象+数据到MVC 6控制器 - ASP.NET 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆