2

I am able to pass single file as System.Web.HttpPostedFileBase but when i pass same as array of files i am getting null in controller's action.

I've tried sending array of files.

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

Controller:

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

Need to get array of files. Thanks in advance.

2
  • Should it be data:FileData instead? Commented Apr 22, 2019 at 16:16
  • 1
    post your html too Commented Apr 22, 2019 at 16:18

3 Answers 3

2

Thanks for the effort guys. I found the solution. I just need to keep append files for same key "Attachments". Now i am able to get as array of 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); 
                }
            });    
Sign up to request clarification or add additional context in comments.

Comments

0

Try It:

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

Comments

0

This is how I used to post array data from javascript to mvc controller, this is going to be a little bit lengthy but I've explained each line with comment, I hope this may help you

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


Controller:

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

}

and here is the model class

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

}

1 Comment

ps> if someone can simplify my logic, please do let me know, it would be much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.