【问题标题】:Send array of files from Jquery ajax to controller action将文件数组从 Jquery ajax 发送到控制器操作
【发布时间】:2019-04-22 16:11:04
【问题描述】:

我能够将单个文件作为 System.Web.HttpPostedFileBase 传递,但是当我传递与文件数组相同的文件时,我在控制器的操作中得到空值。

我已尝试发送文件数组。

HTML:

        <input type="file" id="Attachment1">
        <input type="file" id="Attachment2">
        <input type="file" id="Attachment3">
        <input type="file" id="Attachment4">
        <input type="file" id="Attachment5">

Javascript:

 var FileData = []; 
 $('input').each(function () {
                var type = $(this).attr("type");
                if (type == "file") {
                    FileData.push($(this).get(0).files[0]);
                }
            });    
var Data = new FormData();
Data.append("Attachments", FileData);         
if (url != '') {
    $.ajax({
        url: url,
        data: Data,
        type: "POST",
        contentType: false,
        processData: false,
        success: function (data) {
            alert("Saved successfully");
        }
    });
}

控制器:

public ActionResult InsertDetails(System.Web.HttpPostedFileBase[] Attachments)
{
   return Json(new { Success = false });
}

需要获取文件数组。提前致谢。

【问题讨论】:

  • 应该是 data:FileData 吗?
  • 也发布你的html

标签: c# jquery ajax asp.net-mvc httppostedfilebase


【解决方案1】:

感谢你们的努力。我找到了解决方案。我只需要保留相同键“附件”的附加文件。现在我可以得到 HttpPostedFileBase 的数组。

 var Data = new FormData();
 $('input').each(function () {
                var type = $(this).attr("type");
                if (type == "file") {
                    var FileData = $(this).get(0).files[0]);
                    Data.append("Attachments", FileData); 
                }
            });    

【讨论】:

    【解决方案2】:

    试试看:

        var Files = new FormData();    
    
             $('input').each(function () {
             var type = $(this).attr("type");
              if (type == "file") {
    Files .append("Attachment"+$(this).attr("id"), $(this).get(0).files[0]);   
                            }
                        });    
    
            if (url != '') {
                $.ajax({
                    url: url,
                    data: Files ,
                    type: "POST",
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        alert("Saved successfully");
                    }
                });
            }
    

    【讨论】:

      【解决方案3】:

      这就是我过去将数组数据从 javascript 发布到 mvc 控制器 的方式,这会有点冗长,但我已经用注释解释了每一行,希望对你有帮助

      javascript:

      var formData = new FormData(); //declare formData
      var arrayData = []; //declare array and push/append your data to it.
      
      var name="abc";
      arrayData.push(name);
      
      //setting ArrayData to Json Object
      var AllData = {
                  getUserData: arrayData
              };
      
      //appending Json Object to formdata with the key "mydata"
      formData.append("mydata", JSON.stringify(AllData));
      
      //sending formdata through ajax request
      $.ajax({
            type: "POST",
            url: yourURLHere,
            processData: false,
            contentType: false,
            data: formData,
            cache: false,
            success: function (data) {
                  //your program logic here
            }
      });
      
      
      

      控制器:

      public async Task<HttpResponseMessage> SaveResponse()
      {
      
      //receiving json data from the key "mydata" we set earlier 
      var getData = HttpContext.Current.Request.Params["mydata"];
      
      //deserialize json object (you will need Newtonsoft.Json library)
      var model = JsonConvert.DeserializeObject<MyModel>(getData);
      
      //you will get all of your data in model variable
      //do what you want to do with that data
      
      }
      

      这是模型类

      public class MyModel
      {
         //**dataList** will receive array data sent from javascript  
         public List<MyModel> dataList = new List<MyModel>();
      
         //Remember, whatever your push to array in javascript, should be declared here.
         public string name {get;set;}   
      
      }
      

      【讨论】:

      • ps> 如果有人可以简化我的逻辑,请告诉我,不胜感激。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2019-06-21
      相关资源
      最近更新 更多