AngularJS - ajax - MVC file multiple upload without form data

落爺英雄遲暮 提交于 2019-12-12 18:37:57

问题


I am using Flow.JS http://flowjs.github.io/ng-flow/ for file upload.

My requirement is such that I will have to send the following data all in one save button click

  1. multiple files
  2. two string values alongwith the files.

The following way works fine.

Upload ajax call

    $scope.UploadFiles = function (flows) {
    var data = new FormData();
    $.each(flows.files, function (i, flowfile) {
        data.append('file' + i, flowfile.file);
    });

    data.append('message', $scope.Subject);
    data.append('subject', $scope.Message);

 $.ajax({
        url: 'url\savedata',
        data: files,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST'
    }); 

}

And my MVC conroller

    public JsonResult Savedata()
    {

        var httpRequest = System.Web.HttpContext.Current.Request;
        if(httpRequest.Files.Count != 0)
        {
            var collection = 0;
            foreach (string file in httpRequest.Files)
            {
              //manipulate file data
            }
        }

         var message = httpRequest.Forms['message'];
         var subject= httpRequest.Forms['subject'];

    }

All this works fine. I want to know if there is a better way to do this instead of using form data and possibly send all this data using a data model instead, since I need that for some MVC data validations.

来源:https://stackoverflow.com/questions/33864860/angularjs-ajax-mvc-file-multiple-upload-without-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!