danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.6k forks source link

Passing array as param to file upload #2090

Open kj1981 opened 5 years ago

kj1981 commented 5 years ago

Using angularjs 1.3 and C# .net core web api

I have a ng-file-upload which being used to upload file. When the upload method is called I want to pass in an extra array of some data to the upload method which is then received by the method at my web api. Here is my ng-file-upload

factory.upload = function (file, myArray) {
    var url = '{0}upload'.format(apiPath)
    return Upload.upload({
        url: url,
        arrayKey: '',
        data: { file: file, myArray: myArray}
    }).then(function (res) {

        return res;
    });
};

Below is my webapi:

  [HttpPost("upload")]
   public async Task<IActionResult> FileUpload(IFormFile file, List<ArrayItem> myArray)
   {
        //code
   }

And finally here is the array which I am trying to pass along with upload to my webapi:

 [
   {
     id: "1",
     name: "steve"
   },
   {
     id: "2",
     name: "adam"
   }
  ]

The issue is in my webapi, the myArray parameter which is to accept the array from the UI is always null. I searched online and its mentioned to add

arrayKey: '' or arrayKey: '[]'

But still doesnt works. Any inputs?