2012-01-18 28 views
0

我处于一种情况,我有两个站点,一个是另一个的子域。一个站点将图像上传到文件夹,并将对图像的引用保存到数据库,该数据库可由其他站点访问。asp.net mvc:如何跨域共享图像文件

我需要做的是访问这两个网站中的图像。

我曾考虑将这些文件保存在两个站点都可以访问的文件夹中,但不知道如何引用这些图像,而没有显示完整服务器路径的令人讨厌的src属性。

感谢

回答

2

你可以写一个控制器的动作,这将有助于他们:

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中运行的帐户授予您存储图像所在的文件夹所需的权限,否则,如果此文件夹超出此范围,将无法读取它们应用程序根。

+0

可爱,那就做吧! – Sergio 2012-01-18 10:05:30