Anil  Panwar
Anil Panwar

Reputation: 2622

Post multi files with other data from angular to MVC controller

I am trying to post multiple files with other data in the form.

I am not getting the files with the data at back end, I tried Request.Form["model"] and able to get the data of the form but not the files, so i am after the list of the files so that i can save them at the server folder.

My angular post code as

$http({
            method: 'POST',
            url: baseURL + "/AddToCart",
            headers: { 'Content-Type': undefined },

            transformRequest: function (data) {             
                var formData = new FormData();
                formData.append("model", angular.toJson(data.model));
                for (var i = 0; i < data.model.Item.length; i++) {                  
                    formData.append("file" + i, data.model.Item[i].Image);
                }
                return formData;
            },
            data: { model: model }
        }).
        success(function (data, status, headers, config) {
            alert("success!");
        }).
        error(function (data, status, headers, config) {
            alert("failed!");
        });     
    }

Code in MVC controller as

public JsonResult AddToCart()
        {
            try
            {

                string documentContents;
                using (Stream receiveStream = Request.InputStream)
                {
                    using (StreamReader readStream = new StreamReader(receiveStream, Request.ContentEncoding))
                    {
                        documentContents = readStream.ReadToEnd();
                    }
                }
}

Upvotes: 1

Views: 70

Answers (1)

Avinash Raikwar
Avinash Raikwar

Reputation: 88

Use Like This

            var x = Request.Files;
            var y = Request.Form["model"];

Upvotes: 1

Related Questions