Jquery将数据传递给mvc4中的ajax函数

时间:2013-12-18 11:32:22

标签: jquery ajax

我已经开发了ASP.NET MVC4文件上传它工作正常但我有一个问题我需要将参数 Folderid 传递给控制器​​但不幸的是我无法在控制器中获取folderId。能不能请你尽快帮助我

我的代码

$(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 + '%');
        });
    }); 

我的控制器代码

[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");
        }

1 个答案:

答案 0 :(得分:1)

编辑也许尝试在fileupload调用中将数据更改为formData,如下所示:

formData: { name: Folderid  },

取自here