why images are not loaded despite of correct url in IIS 7?

298 Views Asked by At

this is the code to read the complete path with name of all files (images) from the folder inside the project

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

this code is to load the first image to cshtml view file

 <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
2

There are 2 best solutions below

0
On

Like the error message is telling you, you are not allowed to do that. Due to security reasons, most browsers simply will not allow the use of file://. You need to host it with your application and load it from the web server.
Have a look at this great SO answer for an explanation and proposed solution to the problem.

0
On

This error was due to using the .physical path instead of virtual in following code

@{ 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;"  />

I fixed it by replacing the physical path with virtual

 @{ 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;"  />