亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將文件數組從 Jquery ajax 發送到控制器操作

將文件數組從 Jquery ajax 發送到控制器操作

C#
喵喔喔 2022-12-24 14:18:39
我能夠將單個文件作為 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 });}需要獲取文件數組。提前致謝。
查看完整描述

3 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

我找到了解決方案。我只需要為相同的鍵“附件”保留附加文件?,F在我可以獲取 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); 

                }

            });    


查看完整回答
反對 回復 2022-12-24
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

嘗試一下:


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

                }

            });

        }


查看完整回答
反對 回復 2022-12-24
?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

這就是我以前將數組數據從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;}   


}


查看完整回答
反對 回復 2022-12-24
  • 3 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號