0

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");
        }
4
  • Found this: stackoverflow.com/questions/17934689/… Which may be of some use. Also, I'd suggest mentioning the library you're using, fileUpload is not a jQuery function. Commented Dec 18, 2013 at 11:42
  • @Swires, Could you pls specify where is my worng.. please Commented Dec 18, 2013 at 11:47
  • 1
    See answer for possible fix, but I meant that fileUpload is a seperate library github.com/blueimp/jQuery-File-Upload/wiki/… and should be mentioned as such in the question. When I read it first I thought 'I didn't know jQuery handled file uploads!?' Commented Dec 18, 2013 at 11:52
  • @ Swires, Got It great thanks...you have saved my lot of time i really appreciate you i have used like this formData : {name: Folderid}, Commented Dec 18, 2013 at 12:08

1 Answer 1

1

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

formData: { name: Folderid  },

Taken from here.

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried to use controller like this but i got Null reference error in 'Request.Form["name"]'
this is not working string FolderId = Request.Form["name"]. You have commented link is working you mentioned this link github.com/blueimp/jQuery-File-Upload/wiki/… so if you edit answer make as this link i will tick answer

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.