2012-01-18 52 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