1

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();
                    }
                }
}
3
  • What is error you are facing Commented Aug 28, 2017 at 14:00
  • I am not getting the files with the data at backend, 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. Commented Aug 28, 2017 at 14:01
  • just try finding it in current context HttpContext.Current.Request.Params["model"] Also you nee to apply this on your method [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] Commented Aug 28, 2017 at 14:08

1 Answer 1

1

Use Like This

            var x = Request.Files;
            var y = Request.Form["model"];
Sign up to request clarification or add additional context in comments.

Comments

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.