troubleshooting Question

I am returning request failed but the save is working. What is wrong with this?

Avatar of Natavia Finnie
Natavia FinnieFlag for United States of America asked on
4 Comments1 Solution125 ViewsLast Modified:
I am trying to upload multiple files. Everything seems to be working just fine but my controller is saying that the request failed. What am I doing wrong?

$(document).ready(function () {
    $('#RequestDetailFiles').on('click', function () {
        var data = new FormData();

        var files = $("#DetailFileUpload").get(0).files;

        // Add the uploaded image content to the form data collection
        if (files.length > 0) {
            for (i = 0; i < files.length; i++) {
                data.append("UploadedImage_" + i, files[i]);

        $("#RequestDetailUploaded").append('<p>Uploaded Files</p>');
        $("#RequestDetailUploaded").append('<ul id="DetailAppended">');
        for (var i = 0; i < files.length; i++)
            $("#DetailAppended").append('<li>' + files[i].name + '</li>');

        //append other form data.
        var requestId = $("#RequestId").val();
        data.append("Type", "Request Details");
        data.append("TypeId", "1");
        data.append("RequestId", requestId);

        // Make Ajax request with the contentType = false, and procesDate = false
        var ajaxRequest = $.ajax({
            type: "POST",
            url: "/uploadfile/uploadfile",
            contentType: false,
            processData: false,
            data: data,
            success: function (data) {
                //data is your result from controller
                if (files.success) {
            error: function (xhr) {

        ajaxRequest.done(function (xhr, textStatus) {
            // Do other operation

        public ActionResult UploadFile()
            RequestContext dbContext = new RequestContext();
            Attachment thisAttachment = new Attachment();

            var attachments = new List<Attachment>();

            if (HttpContext.Request.Files.AllKeys.Any())
                for (var i = 0; i < HttpContext.Request.Files.Count; i++)
                    var httpPostedFile = HttpContext.Request.Files["UploadedImage_" + i];

                    var typeId = HttpContext.Request.Form["TypeId"];
                    var type = HttpContext.Request.Form["Type"];
                    var requestId = HttpContext.Request.Form["RequestId"];

                    if (httpPostedFile != null)
                        //Get the complete file path
                        var fileSavePath = Path.Combine(HttpContext.Server.MapPath("~/Attachments"), httpPostedFile.FileName);

                        thisAttachment = new Attachment()
                            Filename = httpPostedFile.FileName,
                            Path = fileSavePath,
                            TypeId = Convert.ToInt32(typeId),
                            CreatedOn = DateTime.Today,
                            RequestId = int.Parse(requestId)


                        // Save the uploaded file to "UploadedFiles" folder
           return View();
Miguel Oz
Senior Software Engineer
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros