Displaying Images in .NET application, that are out of project folder.
Trying to display an image, which is out of a root folder on my desktop. I am using src="@Url.Content()" to display the image, the ones that are in my root folder works just fine, but the ones that are on my desktop do not, the path is being changed inside the app, so it can fit the users needs.
<img src="@Url.Content("C:/SP00000014000.png")" onerror="this.onerror=null;
this.src='@Url.Content("~/img/stockImg.png")'" width="164" height="164" alt="...">
This is inside my cshtml file.10 Replies
Well, yes, Kestrel doesn't serve your desktop, it only serves your static files
You could maybe write an API endpoint that would read the bytes from disk and pass them through with an appropriate MIME type and all
Alternatively... just don't do that
When you deploy your application there won't be any "my desktop"
Well, that is a problem, because this is a app that is developed for a company that does not want normal conventional database, so it uses excel as a "database" and the pictures are somewhere inside a directory on windows server. The app is running locally and the data from excel are being loaded normally, maybe I could try to connect the directories somehow, so they mirror each other? Or write a code, that copies all the images from that one directory to my root directory?
My condolences that you have to work with garbage like that
I guess you could try adding another directory as the static files directory
Well, I already had to do some backloops to have that excel work as I intended, also they can edit it while the app is running, so my app has to load it all again every 5 minutes or so 😄
I will try to add an img folder to my root, and on my model or service layer I will just pull the images in, it is a messy thing to do, but it should work.
One workaround would be a symlink ig
symlink? Never heard of it.
It lets you create a symbolic link to a directory
A portal, so to speak, that lets you use
/some-symlink-dir
as it was the directory you link toAhh, well I could try that, but would it work while the app would be published? Tons of stuff worked while I was debugging, but while I publish I always cross my fingers 😄
Maybe
I will try some things, and post how it went.
Sadly those workarounds are pain and are hardly used ever again
public class ImageCopyService
{
public void CopyPngImages(string sourceFolder, string destinationFolder)
{
// Zkontrolujte, zda zdrojová složka existuje
if (!Directory.Exists(sourceFolder))
{
Console.WriteLine("Zdrojová složka neexistuje.");
return;
}
// ProjdÄ›te vÅ¡echny soubory s pÅ™Ãponou ".png" ve zdrojové složce
string[] pngFiles = Directory.GetFiles(sourceFolder, "*.png");
foreach (string filePath in pngFiles)
{
// ZÃskánà názvu souboru z cesty
string fileName = Path.GetFileName(filePath);
Console.WriteLine(fileName);
// VytvoÅ™enà cesty pro cÃlovou složku
string destinationPath = Path.Combine(destinationFolder, fileName);
// KopÃrovánà souboru do cÃlové složky
File.Copy(filePath, destinationPath, true);
}
}
}
Created a service that Copies the images from the Source file to destination file, the destination file being my root folder, works like a charm 😄