Search code examples
javascriptangularjsasp.net-mvcflow-js

Flow Js - upload files as array


I am trying to use FlowJS angular plugin for a upload functionality and I need to tweak it a little. I will have to files of all types

I am using ASP.NET MVC.

.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
 target: '',
 permanentErrors: [500, 501],
 maxChunkRetries: 1,
 chunkRetryInterval: 5000,
 simultaneousUploads: 1
};

My input button

<input type="file" flow-btn />

My upload button

  <input type="button"  ng-click="uploadFiles($flow)">

And the function

 $scope.uploadme = function (flows) {
    flows.upload();
 });

My mvc controller

  [HttpPost]
    public string UploadFile(HttpPostedFileBase file)
    {
        int fileSizeInBytes = file.ContentLength;
        MemoryStream target = new MemoryStream();
        file.InputStream.CopyTo(target);
        byte[] data = target.ToArray();
        return "";
    }

This works fine, but when I upload multiple files, the controller is hit every time for a file. I need to find a way to send all files to the controller at once, some thing like

    public string UploadFile(HttpPostedFileBase[] file)
    {
    }

Any ways I can achieve this ?


Solution

  • You don't need something like UploadFile(HttpPostedFileBase[] file) in your controller.

    Just Create Controller

    public string UploadFile()
    {
      var httpRequest = HttpContext.Current.Request;
      //httpRequest.Files.Count -number of files
      foreach (string file in httpRequest.Files)
      {
          var postedFile = httpRequest.Files[file];
          using (var binaryReader = new BinaryReader(postedFile.InputStream))
          {
             //Your file
             string req = System.Text.Encoding.UTF8.GetString(binaryReader.ReadBytes(postedFile.ContentLength));
    
          }
    }
    }