使用MVC中数据库中的URL从文件夹加载图像(Load image from a folder using URL from database in MVC)

编程入门 行业动态 更新时间:2024-10-17 11:27:07
使用MVC中数据库中的URL从文件夹加载图像(Load image from a folder using URL from database in MVC)

在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="

本文发布于:2023-07-16 13:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   文件夹   图像   加载   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!