【发布时间】:2014-05-09 15:20:30
【问题描述】:
我检查了this 处理相同问题的问题,但对我没有用。 我实际上上传了一张图片,但是当我单击加载按钮以显示图片时,它会将我重定向到另一个页面,但我想在当前页面中显示它。 你知道如何解决这个问题吗? 这是我的控制器
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content"), fileName);
file.SaveAs(path);
return File(path, "image/jpeg");
}
在我看来
@using(Html.BeginForm("Index","Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<input style="margin-left:40px;cursor:pointer;" type="file" name="file" id="upload1" />
<input type="submit" style="margin-left:40px;cursor:pointer;" id="load1" value="Upload" />
<img src="myimage" alt="Logo" />
}
【问题讨论】:
-
与其返回文件,不如重定向到
HttpGet版本的Index()? -
另一种选择是创建一个接受文件的单独方法,通过 ajax 调用它,然后只返回图像的路径,以便您可以插入 img 标签
标签: c# asp.net-mvc-4 razor image-uploading