【问题标题】:asp.net mvc: How to share images file across domainsasp.net mvc:如何跨域共享图像文件
【发布时间】:2012-01-18 18:09:05
【问题描述】:

我的情况是我有两个站点,一个是另一个的子域。一个站点将图像上传到一个文件夹,并将对该图像的引用保存到数据库中,另一个站点可以访问该数据库。

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

我考虑将文件保存在公共 html 文件夹之外的两个站点都可以访问的文件夹中,但我不确定如何在没有显示完整服务器路径的讨厌 src 属性的情况下引用这些图像。

谢谢

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    你可以编写一个控制器动作来为他们服务:

    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 中运行应用程序的帐户授予存储图像的文件夹所需的权限,否则如果此文件夹不在应用程序根目录。

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 1970-01-01
      • 2017-10-13
      • 2013-08-02
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      相关资源
      最近更新 更多