asp.net mvc-5 HttpPostedFileBase 为空 [英] asp.net mvc-5 HttpPostedFileBase is null
问题描述
我正在使用 asp.net mvc-5 HttpPostedFileBase
处理文件上传,但它显示我在选择图像后 HttpPostedFileBase 为空
这是我的代码
<button type="submit" class="btn btn-primary col-sm-5"><span class="glyphicon glyphicon-plus"></span>按钮>
和我的控制器
[HttpPost]公共 ActionResult 插入(HttpPostedFileBase 文件,quotecompanyquotecomp){var allowedExtensions = new[] {.Jpg"、.png"、.jpg"、jpeg"};如果(文件==空){ViewBag.message = "请选择图片";返回视图();}别的 {ViewBag.message = "图片上传成功";返回视图();}}
<块引用>
if (file == null)(在我上传 png 图像后,在这一行 (file
) 显示我为 null)
这段代码有什么问题?
检查表单属性
人们常犯的一个错误是缺少表单标签中的以下部分:
同样在 MVC 中,您的表单结构可能看起来像这样
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @角色=形式"}))
i am messing around with file uploading using asp.net mvc-5 HttpPostedFileBase
but it is showing me HttpPostedFileBase is null after i am selecting the image
here is my code
<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>
and my controller
[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();
}
}
if (file == null)(on this line (
file
) is showing me null after i am uploading png image)
what is wrong in this code?
Check Form Attributes
A common mistake people make is missing the following part in the form tag:
<form enctype="multipart/form-data">
</form>
Also in MVC your form structure could look something like this
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @role="form" }))
这篇关于asp.net mvc-5 HttpPostedFileBase 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!