asp.net mvc-5 HttpPostedFileBase为null [英] asp.net mvc-5 HttpPostedFileBase is null
本文介绍了asp.net mvc-5 HttpPostedFileBase为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用文件上传使用asp.net mvc-5 HttpPostedFileBase
,但它显示我HttpPostedFileBase为空后我选择图像
这里是我的代码
< input type =filetitle =search imagefile-model =profileimageid =allfilepathname =file/>
< button type =submitclass =btn btn-primary col-sm-5>
< span class =glyphicon glyphicon-plus>< / span>
< / button>
和我的控制器
[HttpPost]
public ActionResult insert(HttpPostedFileBase file,quotation company quotecomp)
{
var allowedExtensions = new [] {
.Jpg,。 png,.jpg,jpeg
};
if(file == null)
{
ViewBag.message =请选择图片;
return View();
}
else {
ViewBag.message =图片已成功上传;
return View();
lockquote
如果(文件== null)(在这一行(文件
)显示我空我后上传PNG图像)
这段代码有什么错误?
解决方案
检查表单属性
人们犯的一个常见错误是在表单标记中缺少以下部分:
< form enctype =multipart / form-data>
< / form>
在MVC中,您的表单结构可能看起来像这样
@using(Html.BeginForm(Action,Controller,FormMethod.Post,new {@ enctype =multipart / form-data,@id = myForm,@ role =form}))
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为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文