将文件数组从 Jquery ajax 发送到控制器操作

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


我试过发送文件数组。


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

Java脚本:


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

}

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


喵喔喔
浏览 83回答 3
3回答

千巷猫影

我找到了解决方案。我只需要为相同的键“附件”保留附加文件。现在我可以获取 HttpPostedFileBase 数组。&nbsp;var Data = new FormData();&nbsp;$('input').each(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var type = $(this).attr("type");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (type == "file") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var FileData = $(this).get(0).files[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data.append("Attachments", FileData);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;

慕尼黑的夜晚无繁华

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

qq_笑_17

这就是我以前将数组数据从javascript发布到mvc 控制器的方式,这会有点冗长,但我已经用注释解释了每一行,我希望这可以帮助你javascript:var formData = new FormData(); //declare formDatavar arrayData = []; //declare array and push/append your data to it.var name="abc";arrayData.push(name);//setting ArrayData to Json Objectvar AllData = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getUserData: arrayData&nbsp; &nbsp; &nbsp; &nbsp; };//appending Json Object to formdata with the key "mydata"formData.append("mydata", JSON.stringify(AllData));//sending formdata through ajax request$.ajax({&nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; url: yourURLHere,&nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; data: formData,&nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your program logic here&nbsp; &nbsp; &nbsp; }});控制器:public async Task<HttpResponseMessage> SaveResponse(){//receiving json data from the key "mydata" we set earlier&nbsp;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{&nbsp; &nbsp;//**dataList** will receive array data sent from javascript&nbsp;&nbsp;&nbsp; &nbsp;public List<MyModel> dataList = new List<MyModel>();&nbsp; &nbsp;//Remember, whatever your push to array in javascript, should be declared here.&nbsp; &nbsp;public string name {get;set;}&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP