日期:2014-05-16  浏览次数:20513 次

ASP.NET MVC中使用AJAX.BeginForm 无法得到文件?
@using (Ajax.BeginForm("UpLoadFile", "CourseCenter", new AjaxOptions { HttpMethod = "POST" }, new { enctype = "multipart/form-data" }))

Request.Files[upload];  count始终是0
------解决方案--------------------
http://stackoverflow.com/questions/19042116/ajax-beginform-in-mvc-to-upload-files
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案--------------------
这个时候需要换个方法获取文件
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);
        }
    }

------解决方案--------------------
你看看AJAX的BeginForm,实际post的数据
------解决方案--------------------
AJAX肯定是得不到文件的,安全性问题.<