0

这是从项目内的文件夹中读取所有文件(图像)名称的完整路径的代码

 @{ string[] imgfiles = Directory.GetFiles(@"D:\MVClearningProjects\Demo\Demo\Property_Data\Images\" + item.Property_ID, "*.*");}

此代码是将第一个图像加载到 cshtml 视图文件

 <img src="@Url.Content(imgfiles[0])" alt="carousel bootstrap first" readonly style="width:200px; height:120px;"  />
//not allowed local resources when I run this project  usig ISS
4

2 回答 2

0

此错误是由于在以下代码中使用 .physical 路径而不是 virtual

@{ string[] imgfiles = Directory.GetFiles(@"D:\MVClearningProjects\Demo\Demo\Property_Data\Images\" + item.Property_ID, "*.*");}
 <img src="@Url.Content(imgfiles[0])" alt="carousel bootstrap first" readonly style="width:200px; height:120px;"  />

我通过用虚拟替换物理路径来修复它

 @{ string[] imgfiles = Directory.GetFiles(@"D:\MVClearningProjects\Demo\Demo\Property_Data\Images\" + item.Property_ID, "*.*");
            for (int i = 0; i < imgfiles.Length; i++)
            {//converting physical address into virtual
                imgfiles[i]= imgfiles[i].Replace(@"D:\MVClearningProjects\Demo\Demo", "~").Replace(@"\", "/");
            }
        }
<img src="@Url.Content(imgfiles[0])" alt="carousel bootstrap first" readonly style="width:200px; height:120px;"  />
于 2016-02-04T18:58:11.770 回答
0

就像错误消息告诉你的那样,你不能这样做。由于安全原因,大多数浏览器根本不允许使用file://. 您需要使用您的应用程序托管它并从 Web 服务器加载它。
看看这个很棒的SO answer以获得解释并提出问题的解决方案。

于 2016-02-04T18:34:26.307 回答