【发布时间】:2014-02-19 16:44:10
【问题描述】:
在我的 .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
);
}
【问题讨论】:
-
请求是否到达控制器?如果是,你能展示它的代码吗?
-
不,它没有到达...我将显示我的控制器代码
-
此外,您似乎有自定义路线。也很高兴见到他们
-
当我点击上传按钮时,它会刷新页面并在地址栏中更改 http:// /Home/FileUpload?CusId=1&Name=Nidheesh&CusId=1&Name=Nidheesh&file=Customer.JPG#
-
某事导致您的表单通过 GET 提交,即使您最初将其设置为 POST。由于 GET 操作只是返回视图,因此您永远无法到达任何地方。它可能是某个地方的一些流浪JS。
标签: asp.net asp.net-mvc image razor file-upload