Asp.net mvc Request.files在MVC文件上载中为空

Asp.net mvc Request.files在MVC文件上载中为空,asp.net-mvc,file-upload,Asp.net Mvc,File Upload,我也有同样的问题 @使用(Html.BeginForm(“CreateRequest”、“SupportRequest”、FormMethod.Post、new{id=“frmStemplate”、enctype=“multipart/formdata”})) { } 函数DocumentUpload() { var BrowseFile=$('#FirstFile').val(); 如果(BrowseFile!=null&&BrowseFile!=“”){ 警报(浏览文件); $.ajax({

我也有同样的问题

@使用(Html.BeginForm(“CreateRequest”、“SupportRequest”、FormMethod.Post、new{id=“frmStemplate”、enctype=“multipart/formdata”}))
{
}
函数DocumentUpload()
{
var BrowseFile=$('#FirstFile').val();
如果(BrowseFile!=null&&BrowseFile!=“”){
警报(浏览文件);
$.ajax({
键入:“POST”,
数据类型:“json”,
url:'@url.Content(“~/SupportRequest/UploadFiles”)?fileElementId='+BrowseFile,
成功:功能(数据){
警报('Hi');//调试器;
如果(data.Result==“成功”){
警报(“Hi”);
}
否则{
ShowInfo(“文件上传成功”);
}
}
});
}
}
在控制器方面,我有:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFiles(string fileElementId, FormCollection formColl)
{
    var FirstFile = Request.Files;
    foreach (string upload in Request.Files)
    {
        if (!Request.Files[upload].HasFile()) continue;
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string filename = Path.GetFileName(Request.Files[upload].FileName);
        Request.Files[upload].SaveAs(Path.Combine(path, filename));
    }
    return Json(new { Filename = "" });
}
但是我的
Request.Files
总是
null


我尝试了几件事,比如将代码更改为
Request.Files[“FirstFile”]
,等等。每次,文件集合都是空的。

因为您正在创建数据发布,所以您需要发布文件数据。见此帖:


您需要在控制器操作参数中使用
HttpPostedFileBase
,以获取已发布的文件数据


请阅读Phil Haack的全文

我使用表单集合的不同方式,可以轻松上传文件。就像你在cshtml页面的begin表单中添加了这一行一样

public ActionResult Create([Bind(Include="Id,Name,Pic,ActiveStatus")] FormCollection form)
    {

        string Pic = "";
        var file = Request.Files[0];

        if (file.ContentLength > 0)
        {
            Pic = string.Format("~/Uploads/Category/{0}", file.FileName);
            file.SaveAs(HttpContext.Server.MapPath(Pic));

        }

        ItemCategory obj = new ItemCategory
        {
            Name = form["Name"].ToString(),
            Pic = file.FileName

        };

        db.ItemCategories.Add(obj);
        db.SaveChanges();
        return RedirectToAction("Index");

这个链接似乎断了。