在ASP.NET MVC
申请中,file
已成功保存到文件夹,其URL
已保存到SQL database
。使用此file
在文件夹中的浏览器中加载URL
时出现问题。代码实现是:
[HttpPost]
[ActionName("UploadPhoto")]
public ActionResult UploadPhoto(HttpPostedFileBase photoPath)
{
var fileName = Path.GetFileName(photoPath.FileName);
string path;
if (photoPath.ContentLength > 0)
{
path = Path.Combine(Server.MapPath("~/Images/photos"), fileName);
photoPath.SaveAs(path);
return RedirectToAction("CreateWithImage", new { path = path });
}
return View();
}
public ActionResult CreateWithImage(string path)
{
employee em = new employee();
em.districts = new SelectList(hc.districts, "name", "name");
em.photoPath = path;
return View(em);
}
file(image)在视图中呈现为:
@model HRMS.Models.employee
<dd>
@Html.Image(@Model.photoPath)
</dd>
@Html.Image
的扩展方法实现是:
namespace HRMS.CustomeHTMLHelper
{
public static class CustomHtmlHelper
{
public static IHtmlString Image(this HtmlHelper helper,string src)
{
TagBuilder tb = new TagBuilder("img");
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
}
}
调用View时,我看到图像的链接断开。加载页面(View)的HTML(具有正确的文件路径)被视为:
<dd>
<img src="/App_Data/photos/1.png" />
</dd>
当我尝试在浏览器中运行图像的物理路径,即<b>Requested URL</b> http://localhost:57852/App_Data/photos/1.png
时,它会抛出HTTP Error 404.8 - Not Found
错误。
我哪里错了?请帮忙。