C
C#•12mo ago
Devastorian

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
Angius
Angius•12mo ago
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"
Devastorian
DevastorianOP•12mo ago
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?
Angius
Angius•12mo ago
My condolences that you have to work with garbage like that I guess you could try adding another directory as the static files directory
Devastorian
DevastorianOP•12mo ago
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.
Angius
Angius•12mo ago
One workaround would be a symlink ig
Devastorian
DevastorianOP•12mo ago
symlink? Never heard of it.
Angius
Angius•12mo ago
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 to
Devastorian
DevastorianOP•12mo ago
Ahh, 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 😄
Angius
Angius•12mo ago
Maybe
Devastorian
DevastorianOP•12mo ago
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 😄
Want results from more Discord servers?
Add your server