Reputation: 575
I have images saved on my C drive as follows:
C:\Users\Public\images\employees
I have a view model in my project that contains a property string ImageUrl
. In my Controller, I am fetching the image according to the image name:
var model = new EmployeeViewModel
{
FirstName = employeeDetails.FirstName,
FullName = employeeDetails.FullName,
LastName = employeeDetails.LastName,
ImageUrl = new System.Uri(Path.Combine(@"C:\Users\Public\images\employees", "John.jpg")).AbsoluteUri
};
Then I am passing this model into the View. In my View, I want to display the image as follows:
But I am getting an exception
Not allowed to load local resource: file:// in ASP.NET Core
Can someone please help with this. Also, my application will de deployed to a domain. How can I ensure the image will still display?
Upvotes: 4
Views: 14918
Reputation: 1
You have to create folder within the project because project can only access the local files which are within the current project. You cannot access files out of the scope of the project.
Upvotes: 0
Reputation: 400
You can create virtual directory which will have this phisical location there "C:\Users\Public\images". It is not secure to use external files without add them to project resources. Or add this images to wwwroot which is by default used for project resources.
Virtual directory: https://www.c-sharpcorner.com/article/virtual-directory-inside-of-asp-net-core-app-in-iis/
Upvotes: 0
Reputation: 19
put your images folder inside the wwwroot folder. That's where the static files should be. That worked for me.
<img src="/Imagens/conflito.png" alt="Sample Photo" height="300" />
Upvotes: 1
Reputation: 27793
passing this model into the View. In my View, I want to display the image
If you'd like to display image(s) stored within a folder of C drive
on your server in view page, you can refer to the following code snippet to serve files from expected location.
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(@"C:\Users\Public\images", "employees")),
RequestPath = "/employees"
});
In controller action
var model = new EmployeeViewModel
{
FirstName = employeeDetails.FirstName,
FullName = employeeDetails.FullName,
LastName = employeeDetails.LastName,
ImageUrl = "/employees/John.jpg"
};
For more information about serving static files and PhysicalFileProvider, please check:
Upvotes: 4
Reputation: 64
I created a .Net Fiddle to help demonstrate what I believe you are experiencing, filling in some gaps. It can be found here.
https://dotnetfiddle.net/S6deCz
When tried using various browsers it highlights that the browsers are preventing remote code from accessing files from the local machine.
I would suggest the best way to solve this problem and allow everyone to access it, is to ensure that the employee images are stored on the web server (IIS from the questions and answers above).
I have added ServerImageUrl to the demonstrate how you'd achieve this using a web server located resource.
Hope this helps.
Upvotes: -1