在这里,我的cshtml.cs文件用于上传图片并为我的网站添加图片。
@using (Html.BeginForm("Logo", "Header", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-md-12">
<div class="panel">
<div class="panel-heading">
<h2>Logo</h2>
</div>
<div class="panel-body">
<div class="col-md-6">
<div class="form-group">
<label>Logo Image<span style="color:red;">*</span>:</label>
@Html.HiddenFor(model => model.logoId)
@Html.TextBoxFor(model => model.LogoImage, new { @type = "file", @Id = "Files" })
@Html.ValidationMessageFor(model => model.LogoImage)
</div>
<span style="color:red;">Note:Please Upload Maximum Image size is 6 MB!!</span><br />
<span style="color:red;">Note:Allowed Image size Height: 45px, Width: 192px</span>
<div class="form-group">
<input type="submit" name="submit" value="Upload" class="btn btn-sm btn-primary" />
</div>
</div>
<div class="col-md-6">
<img id="Logo" alt="Logo-Preview" name="LogoImage" class="prev" style="width:192px;height:45px;" />
</div>
</div>
</div>
</div>
}
在这里,当我上传任何文件或图像时,HttpPostedFileBase文件重新调整始终为null。
我尝试了所有从MVC研究中学到的东西,但是没有用,所以我该如何解决。在设计方面,我正在使用模型获取数据并传递数据
[HttpPost]
public ActionResult Logo(HttpPostedFileBase file, LogoViewModel model)
{
if (ModelState.IsValid)
{
if (Helper.IsValidImageFile(file.FileName))
{
LogoMaster objlogo = new LogoMaster();
try
{
if (file != null)
{
string filename = Helper.ToValidFileName(Path.GetFileName(file.FileName));
string path = Path.Combine(Server.MapPath("~/Images/" + filename));
string filepathtosave = "~/Images/" + filename;
file.SaveAs(path);
objlogo.LogoImage = filepathtosave;
}
var IsExist = from i in _DBContext.LogoMasters select i;
if (IsExist.Count() > 0)
{
var Update = _DBContext.LogoMasters.First(x => x.LogoId == IsExist.FirstOrDefault().LogoId);
Update.LogoImage = objlogo.LogoImage;
_DBContext.SaveChanges();
return View();
}
else
{
objlogo.LogoImage = model.LogoImg;
if (model.logoid == null)
_DBContext.LogoMasters.Add(objlogo);
_DBContext.SaveChanges();
ModelState.Clear();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
return View();
}
答案 0 :(得分:4)
这很简单,我犯了一个我看到你的错误,只需将其应用即可运行。
@using (Html.BeginForm("Logo", "Header", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-md-12">
<div class="panel">
<div class="panel-heading">
<h2>Logo</h2>
</div>
<div class="panel-body">
<div class="col-md-6">
<div class="form-group">
<label>Logo Image<span style="color:red;">*</span>:</label>
@Html.HiddenFor(model => model.logoId)
@Html.TextBoxFor(model => model.LogoImage, new { @type = "file", @Id = "Files" })
@Html.ValidationMessageFor(model => model.LogoImage)
</div>
<span style="color:red;">Note:Please Upload Maximum Image size is 6 MB!!</span><br />
<span style="color:red;">Note:Allowed Image size Height: 45px, Width: 192px</span>
<div class="form-group">
<input type="submit" name="submit" value="Upload" class="btn btn-sm btn-primary" />
</div>
</div>
<div class="col-md-6">
<img id="Logo" alt="Logo-Preview" name="LogoImage" class="prev" style="width:192px;height:45px;" />
</div>
</div>
</div>
</div>
}
只需将名称用HttpPostedFileBase文件替换为HttpPostedFileBase LogoImage即可
[HttpPost]
public ActionResult Logo(HttpPostedFileBase LogoImage, LogoViewModel model)
{
...
//your code
...
return View();
}
答案 1 :(得分:1)
在您的视图中像这样的示例添加enctype="multipart/form-data"
<form action="/Account/Register" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
</form>