我正在使用 mvc-5 HttpPostedFileBase
上传文件 asp.net 但它显示我选择图像后 HttpPostedFileBase 为空
这是我的代码
<input type="file" title="search image" file-model="profileimage" id="allfilepath" name="file" />
<button type="submit" class="btn btn-primary col-sm-5">
<span class="glyphicon glyphicon-plus"></span>
</button>
和我的控制器
[HttpPost]
public ActionResult insert(HttpPostedFileBase file, quotationcompany quotecomp)
{
var allowedExtensions = new[] {
".Jpg", ".png", ".jpg", "jpeg"
};
if (file == null)
{
ViewBag.message = "Please Select Image";
return View();
}
else {
ViewBag.message = "Image Uploaded Successfully";
return View();
}
}
如果(文件 == 空((在此行 (
file
( 上传 PNG 图像后向我显示空(
这段代码有什么问题?
检查表单属性
人们常犯的一个错误是表单标签中缺少以下部分:
<form enctype="multipart/form-data">
</form>
同样在 MVC 中,您的表单结构可能如下所示
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @role="form" }))