Sajith
Sajith

Reputation: 856

Jquery passing data to ajax function in mvc4

I have developed ASP.NET MVC4 File upload it is working fine but i have one problem i need to passing parameter Folderid to controller but unfortunately i cant get folderId in controller. could you please help me as soon as possible

My code below

$(document).ready(function () {

        var Folderid = "ab";     

        $('#fileupload').fileupload({
            dataType: 'json',
            url: '/Home/UploadFiles',
            autoUpload: true,
             data: { name: Folderid  },
            done: function (e, data) {
                if (data.result.name == '') {
                    $('.file_name').html('Please Upload valid image...');
                    $('.progress .progress-bar').css('width', 0 + '%');

                }
                else {
                    $('.file_name').html("Uploaded Successfully..[ " + data.result.name + " ]");
                }

            }
        }).on('fileuploadprogressall', function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('.progress .progress-bar').css('width', progress + '%');
        });
    }); 

My controller code below

[HttpPost]
        public ContentResult UploadFiles(string name)
        {

            string FolderId = name;

            var r = new List<UploadFilesResult>();               
            foreach (string file in Request.Files)
            {
                var allowedExtensions = new[] { ".jpg", ".jpeg", ".bmp", ".icon" };

                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                if (!allowedExtensions.Contains(System.IO.Path.GetExtension(hpf.FileName).ToString()))
                {
                    r.Add(new UploadFilesResult()
                    {
                        Name = "",
                        Length = 0,
                        Type = ""

                    });
                }
                else
                {
                    string savedFileName = Path.Combine(Server.MapPath("~/Upload"), Path.GetFileName(hpf.FileName));
                    hpf.SaveAs(savedFileName);
                    r.Add(new UploadFilesResult()
                    {
                        Name = hpf.FileName,
                        Length = hpf.ContentLength,
                        Type = hpf.ContentType
                    });
                }
            }
            return Content("{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}", "application/json");
        }

Upvotes: 0

Views: 211

Answers (1)

ChrisSwires
ChrisSwires

Reputation: 2723

Edit Perhaps try changing data to formData in the fileupload call like so:

formData: { name: Folderid  },

Taken from here.

Upvotes: 1

Related Questions