http://stackoverflow.com/questions/19042116/ajax-beginform-in-mvc-to-upload-files
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

解决方案 »

  1.   

    这个时候需要换个方法获取文件
    Request.Files[upload]是获取字符串的public PartialViewResult Files(HttpPostedFileBase file)
        {
            IEnumerable<string> files;
            if ((file != null) && (file.ContentLength > 0))
            {
                string fileName = file.FileName;
                string saveLocation = @"D:\Files";
                string fullFilePath = Path.Combine(saveLocation, fileName);               
                try
                {
                    file.SaveAs(fullFilePath);
                    FileInfo fileInfo = new FileInfo(fullFilePath);
                    file.InputStream.Read(new byte[fileInfo.Length], 0, file.ContentLength);                    
                }
                catch (Exception e)
                {
                    TempData["FileUpload"] = e.Message;
                    return PartialView();
                }
                files = Directory.GetFiles(@"D:\Files\");
                return PartialView(files);
            }
            else
            {
                files = Directory.GetFiles(@"D:\Files\");
                return PartialView(files);
            }
        }
      

  2.   

    提问前已阅,还在学习中,不太会JQ,不过已用JQ的plugin搞定了,谢谢  
      

  3.   

    Request.Files[upload]这个是回去当前请求中提交的文件的集合吧...现在我碰到的问题是文件集合为空,那也没办法给这个方法传参吧
      

  4.   

    版主可否解释下为什么AJAX的BeginForm抓不到文件但Html的BeginForm却很正常? 谢谢
      

  5.   

    你看看AJAX的BeginForm,实际post的数据
      

  6.   

    AJAX肯定是得不到文件的,安全性问题.
      

  7.   

    其实问题出在这个地方   form中有一个属性 enctype="multipart/form-data",ajax.beginform生成出来的属性中没有enctype,你需要在ajax.beginform(参数,参数,new{id="form1",class='form1',enctype="multipart/form-data"})在一个参数中设置,具体哪一个忘记了,就是设置元素属性的一个参数,自己看看,加上enctype即可
      

  8.   

    Index.cshtml@using (Ajax.BeginForm("Files", "Home", new AjaxOptions() { HttpMethod = "post" }, new { enctype = "multipart/form-data" }))

        @Html.AntiForgeryToken();
        <input type="file" name="file1" /> <br />
        <input type="submit" value="上传" />
    }
    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div><div id="status"></div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>
    <script>
        (function () {        var bar = $(".bar");
            var percent = $(".percent");
            var status = $("#status");        $('form').ajaxForm({
                beforeSend: function () {
                    status.empty();
                    var percentVal = '0%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                uploadProgress: function (event, position, total, percentComplete) {
                    var percentVal = percentComplete + '%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                success: function () {
                    var percentVal = '100%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                complete: function (xhr) {
                    status.html(xhr.responseText);
                }
            });    })();       
    </script>Controller: //分步视图上传文件        [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Files()
            {
                HttpPostedFileBase file = Request.Files[0];
                if (file != null)
                {                    // Verify that the user selected a file
                    if (file != null && file.ContentLength > 0)
                    {
                        // extract only the fielname
                        var fileName = Path.GetFileName(file.FileName);
                        // TODO: need to define destination
                        var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
                        file.SaveAs(path);                }
                }
                ViewBag.file = Path.GetFileName(file.FileName);
                return PartialView();
            }
        }Files.cshtml<ul>    <li>@ViewBag.file</li></ul>运行效果图: