繁体   English   中英

如何将Javascript文件对象+数据发送到MVC 6控制器-ASP.NET 5

[英]How to send Javascript File Object+ Data to MVC 6 Controller - ASP.NET 5

这个Ajax在一个我知道具有文件和属性的函数中。什么也没有发生,我正在拖动文件并将其拖放,但Ajax却没有任何反应。 当我将属性输出到页面时,我看到JS中的函数具有文件,但没有将其发送到控制器。 我认为它不能正常工作,并且在调试索引控制器时,参数中没有文件属性。 Ajax没有给我成功或失败警报消息。

我的目标是将文件中的数据上传到上面,这样我就可以解析它了。

的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);
    }

网页HTML

            <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>

要在控制器中获取文件,您必须发送完整文件。

尝试类似:

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 :(');
            }
        });

控制器

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM