【发布时间】:2012-01-18 18:09:05
【问题描述】:
我的情况是我有两个站点,一个是另一个的子域。一个站点将图像上传到一个文件夹,并将对该图像的引用保存到数据库中,另一个站点可以访问该数据库。
我需要做的是访问两个站点中的图像。
我考虑将文件保存在公共 html 文件夹之外的两个站点都可以访问的文件夹中,但我不确定如何在没有显示完整服务器路径的讨厌 src 属性的情况下引用这些图像。
谢谢
【问题讨论】:
标签: asp.net-mvc
我的情况是我有两个站点,一个是另一个的子域。一个站点将图像上传到一个文件夹,并将对该图像的引用保存到数据库中,另一个站点可以访问该数据库。
我需要做的是访问两个站点中的图像。
我考虑将文件保存在公共 html 文件夹之外的两个站点都可以访问的文件夹中,但我不确定如何在没有显示完整服务器路径的讨厌 src 属性的情况下引用这些图像。
谢谢
【问题讨论】:
标签: asp.net-mvc
你可以编写一个控制器动作来为他们服务:
public ActionResult Image(int id)
{
var image = ... go and fetch the image information from the database (you will need the path to the image on the server and the content type)
string path = image.Path; // could be any absolute path anywhere on your server, for example c:\foo\bar.jpg
string contentType = image.ContentType; // for example image/jpg
return File(path, contentType);
}
然后在你看来:
<img src="@Url.Action("Image", "SomeController", new { id = 123 })" alt="" />
将呈现为:
<img src="/SomeController/Image/123" alt="" />
其中 123 显然是数据库中图像记录的唯一标识符。
还要确保您已将在 IIS 中运行应用程序的帐户授予存储图像的文件夹所需的权限,否则如果此文件夹不在应用程序根目录。
【讨论】: