【问题标题】:How to send Javascript File Object+ Data to MVC 6 Controller - ASP.NET 5如何将 Javascript 文件对象 + 数据发送到 MVC 6 控制器 - ASP.NET 5
【发布时间】:2016-02-01 13:51:37
【问题描述】:

这个 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);
    }

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>

【问题讨论】:

  • 如果您尝试发送实际文件,您需要传递的不仅仅是名称、大小等属性,您还必须发送实际文件对象
  • 谢谢,我需要做的就是获取文件中的内容,以便解析数据。

标签: javascript c# asp.net ajax asp.net-mvc


【解决方案1】:

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

尝试类似:

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

【讨论】:

  • 谢谢,但似乎还是不想用数据填写参数。
猜你喜欢
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
相关资源
最近更新 更多