Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC HttpPostedFIleBase为空_Asp.net Mvc_Httppostedfilebase - Fatal编程技术网

Asp.net mvc ASP.NET MVC HttpPostedFIleBase为空

Asp.net mvc ASP.NET MVC HttpPostedFIleBase为空,asp.net-mvc,httppostedfilebase,Asp.net Mvc,Httppostedfilebase,我的视图中有一个@Html.BeginForm,它允许用户上传图像并添加有关图像的一些细节 以下是视图: @Html.BeginForm("SaveImage", "Listing", FormMethod.Post, new { enctype = "multipart/form-data" }))) { <div id="modalAddImage" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby=

我的视图中有一个
@Html.BeginForm
,它允许用户上传图像并添加有关图像的一些细节

以下是视图:

@Html.BeginForm("SaveImage", "Listing", FormMethod.Post, new { enctype = "multipart/form-data" }))) {
<div id="modalAddImage" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Modal header</h3>
    </div>
    <div class="modal-body">
    <p>Image</p>
    <input type="file" name="file"></input>

     @foreach (Image translation in Model.Listing.Images)
     {
        @Html.TextBoxFor(m => m.Description)
     }
     </div>
     <div class="modal-footer">
     <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
     <button class="btn btn-primary">Save changes</button>
 </div>
</div>
}
当我在
SaveImage
中设置断点时,文件总是空的。我尝试上传大小图片


有人知道我犯了什么大错吗?

你可以这样做:

[HttpPost]
public ActionResult SaveImage(HttpPostedFileBase file)
{
     if (Request.Files.Count > 0)
     {
        foreach (string upload in Request.Files)
        {
           if (upload != null)
           {
              var fileName = Path.GetFileName(file.FileName);
              var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);      
              Request.Files[upload].SaveAs(path);

           }
        }
     }
    return View("Maintain");
}

遇到同样的问题,然后意识到我没有在表单中指定(enctype=“multipart/form data”)

“file”太未定义,无法在此处进行扩展使用。为什么不尝试在输入类型文件和控制器参数上使用“uploadedFile”或类似的东西?检查我的答案在操作中直接使用请求可能是个坏主意,HttpPostedFileBase只是为了避免这种情况,仍然是为什么?@Akash KC如果单击5个图像上载,那么该如何处理此代码?
[HttpPost]
public ActionResult SaveImage(HttpPostedFileBase file)
{
     if (Request.Files.Count > 0)
     {
        foreach (string upload in Request.Files)
        {
           if (upload != null)
           {
              var fileName = Path.GetFileName(file.FileName);
              var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);      
              Request.Files[upload].SaveAs(path);

           }
        }
     }
    return View("Maintain");
}