在我的.NET MVC3 razor PROJECT中,我必须实现图片上传。我的问题是,当我点击上传按钮时,它只是刷新页面。
我的观点代码
@using (Html.BeginForm("UploadPhoto", "Home",FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div class="form-group">
<label class="col-lg-2 control-label">
Customer ID</label>
<div class="col-lg-10">@Html.TextBoxFor(model => model.CusId, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">
Customer Name</label>
<div class="col-lg-10">@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })</div>
</div>
<input type="hidden" id="id" />
<div class="col-md-6">
<div class="form-group">
<label class="col-lg-2 control-label">
DMIT Image</label>
<div class="col-lg-10">
@Html.HiddenFor(model=>model.CusId)
@Html.HiddenFor(model=>model.Name)
<input type="file" id="file" name="file">
<input type="submit" class="btn btn-success" value="Upload" />
</div>
</div>
</div>
}
这是我点击提交按钮时地址栏的更改
http:// /Home/FileUpload?CusId=1&Name=Nidheesh&CusId=1&Name=Nidheesh&file=Customer.JPG#
控制器
[HttpGet]
public ActionResult UploadPhoto()
{
return View();
}
[HttpPost]
public ActionResult UploadPhoto(ElixiCustPro elixi, HttpPostedFileBase uploadfile, int CusId,string Name)
{
try
{
if (uploadfile != null && uploadfile.ContentLength > 0)
{
if ((uploadfile.ContentType == "image/jpeg") || (uploadfile.ContentType == "image/gif") || (uploadfile.ContentType == "image/png"))//check allow jpg, gif, png
{
elixi.Image = new byte[uploadfile.ContentLength];
uploadfile.InputStream.Read(elixi.Image, 0, uploadfile.ContentLength);
var filename = Path.GetFileName(uploadfile.FileName);
var path = Path.Combine(Server.MapPath("~/ElixirFiles/UploadImagesElixir/"), filename);
uploadfile.SaveAs(path);
ecp.Image = new byte[uploadfile.ContentLength];
ecp.ImageUrl = path;
ecp.CustomerName = Name;
ecp.CusId = CusId;
ment.ElixiProData.Add(ecp);
ment.SaveChanges();
return RedirectToAction("ScanManagement");
}
}
}
catch (Exception ex)
{
return View(ex.Message.ToString());
}
return View();
}
aspx.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Login", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
答案 0 :(得分:2)
您的代码中需要进行几项修改才能使其正常工作。
不需要像-int CusId,string Name这样的参数。它们已包含在模型中。
你的HttpPostedFileBase名称应该是'file',它应该等同于input = file type的名称。否则你会得到null。
所以工作代码是 -
[HttpPost]
public ActionResult UploadPhoto(ElixiCustPro elixi, HttpPostedFileBase file)
{
return null;
}
我使用的其他代码部分如下 -
模型 -
public class ElixiCustPro
{
public string Name { get; set; }
public string CusId { get; set; }
}
获取行动 -
[HttpGet]
public ActionResult UploadPhoto()
{
ElixiCustPro p = new ElixiCustPro();
return View(p);
}
查看 -
@model MVC.Controllers.ElixiCustPro
@using (Html.BeginForm("UploadPhoto", "Upload", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div class="form-group">
<label class="col-lg-2 control-label">
Customer ID
</label>
<div class="col-lg-10">@Html.TextBoxFor(model => model.CusId, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">
Customer Name
</label>
<div class="col-lg-10">@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })</div>
</div>
<input type="hidden" id="id" />
<div class="col-md-6">
<div class="form-group">
<label class="col-lg-2 control-label">
DMIT Image
</label>
<div class="col-lg-10">
@Html.HiddenFor(model => model.CusId)
@Html.HiddenFor(model => model.Name)
<input type="file" id="file" name="file">
<input type="submit" class="btn btn-success" value="Upload" />
</div>
</div>
</div>
}
输出 -
答案 1 :(得分:1)
您需要在代码中进行一些修改。