在ASP.NET MVC应用程序中, file已成功保存到文件夹,其URL保存到SQL database 。 URL以Absolute URL格式保存,即(D:\ Visual Studio Projects \ HRMS \ HRMS \ App_Data \ photos \ 5.png)。 使用此URL在文件夹中的浏览器中加载file时出现问题。 代码实现是:
[HttpPost] [ActionName("UploadPhoto")] public ActionResult UploadPhoto(HttpPostedFileBase photoPath) { var fileName = Path.GetFileName(photoPath.FileName); if (photoPath.ContentLength > 0) { var path = Path.Combine(Server.MapPath("~/App_Data/photos"), fileName); photoPath.SaveAs(path); } ViewBag.upload = "Success! Photo was uploaded successfully."; string fpath = Path.Combine(Server.MapPath("~/App_Data/photos"), fileName); TempData["filePath"] = fpath; return RedirectToAction("CreateWithImage", new { path = fpath }); } public ActionResult CreateWithImage(string path) { employee em = new employee(); em.districts = new SelectList(hc.districts, "name", "name"); string fp = Convert.ToString(TempData["filePath"]); em.photoPath = fp; return View(em); }file(image)在视图中呈现为:
@model HRMS.Models.employee <dd> <img src="@Url.Content(@Model.photoPath)" /> </dd>调用View时,我看到图像的链接断开。 加载页面(视图)的HTML(具有正确的文件路径)被视为:
<dd> <img src="D:\Visual Studio Projects\HRMS\HRMS\App_Data\photos\5.png" /> </dd>有人可以记录问题并相应指导吗?
In an ASP.NET MVC application, file is successfully saved to a folder and its URL is saved to SQL database. URL is saved in an Absolute URL form i.e., (D:\Visual Studio Projects\HRMS\HRMS\App_Data\photos\5.png). Having problem in loading file in a browser from folder using this URL. Code implementation is:
[HttpPost] [ActionName("UploadPhoto")] public ActionResult UploadPhoto(HttpPostedFileBase photoPath) { var fileName = Path.GetFileName(photoPath.FileName); if (photoPath.ContentLength > 0) { var path = Path.Combine(Server.MapPath("~/App_Data/photos"), fileName); photoPath.SaveAs(path); } ViewBag.upload = "Success! Photo was uploaded successfully."; string fpath = Path.Combine(Server.MapPath("~/App_Data/photos"), fileName); TempData["filePath"] = fpath; return RedirectToAction("CreateWithImage", new { path = fpath }); } public ActionResult CreateWithImage(string path) { employee em = new employee(); em.districts = new SelectList(hc.districts, "name", "name"); string fp = Convert.ToString(TempData["filePath"]); em.photoPath = fp; return View(em); }file (image) is rendered in a view as:
@model HRMS.Models.employee <dd> <img src="@Url.Content(@Model.photoPath)" /> </dd>When View is called, I see a broken link for the image. HTML(with correct file path) for the loaded page (View) is seen as:
<dd> <img src="D:\Visual Studio Projects\HRMS\HRMS\App_Data\photos\5.png" /> </dd>Can someone note the problem and guide accordingly?
最满意答案
从App_Data文件夹中删除图像,因为它是一个隐藏的段部分,因此拒绝该应用程序访问该文件夹。
只需在app_data文件夹外创建一个新文件夹即可。
您现在可以通过两种方式访问图像
相对路径示例: ../photos/5.png 绝对路径示例:[ http:// localhost:portnumber / photos / 5.png]有用的提示 :运行应用程序后,您可以使用浏览器中的绝对路径查看图像是否可访问。 如果您无法访问它,将告诉您尝试搜索该图像的物理路径。 有了这个,你就可以轻松修复绝对路径。
Remove the Image from App_Data folder because it is a hidden Segment section and hence the application is denied access to that folder.
Just Create a New Folder outside the app_data folder.
You can now access the image in two ways
Relative path Example: ../photos/5.png Absolute path Example: [http://localhost:portnumber/photos/5.png]Helpful Tip:Once you run the application you can use the absolute path in the browser to see if the image is accessible. If you are not able to access it will tell u the physical path where it is trying to search that image. With this you will be easy able to fix the absolute path.
更多推荐
file,photoPath,path,URL,View,电脑培训,计算机培训,IT培训"/> <meta name="
发布评论