How to display image which is stored in local drive?

I have a asp page in which i have to display the image which is stored in my local disk C:
i.e..
C:Program FilesAdrenalinAdrenalinUploadedFilesTemplateFileabc.jpg

how can i do that…i am not able to do so.
the image is not displayed instead it shows a empty image holder with the name of the image as specified and URL as not available.

Please help….

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You will have problems with permissions if the image is outside of the web site folder. Traditionally, web sites run under the NETWORK SERVICE user account, which will limit access to files outside of the folder. You will need to extract the file from a folder with similar access and it is extremely unwise to do so, particularly from Program Files.

You should possibly proxy the file via a web page or web service, which doesn’t expose the fact that the image is served external to the web site. You’ll need to make sure the target folder C:Program FilesAdrenalinAdrenalinUploadedFilesTemplateFile has NETWORK SERVICE Read-access.

eg. create a blank ASP.NET page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageServer.aspx.cs" Inherits="ImageServer" %>

with the code behind:

class ImageServer
{
  void Page_Load(object sender, EVentArgs e)
  {
    Response.ContentType="image/jpeg"; // for JPEG file
    string <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681800111b010b09042e01040d2609050d5528">[email protected]</a>"C:Program FilesAdrenalinAdrenalinUploadedFilesTemplateFileabc.jpg";
    Response.WriteFile(physicalFileName);
  }
}

And test in your browser by going to the URL

http://<localhost>/<website>/ImageServer.aspx

You should get the image.

Then, within the tag, use the URL of the page as your image placeholder:

<img src="ImageServer.aspx" alt="Image served" />

UPDATE:

Looking at your latest comments, I suggest you send a QueryString parameter with some sort of employee code and use that to query the database and get the appropriate filename within the Page_Load() method. Don’t send the filename as part of the QueryString.

Method 2

What you’re trying to do can work, but it’s highly discouraged in Web development. Putting the permissions problems aside (which are very serious), you can never assume that your images will be available in the absolute path you provided on drive C:.

What you should do, is create a folder inside your website directory and use it to store the images, and use relative links instead of absolute links.

Even if you manage to make absolute links work now, prepare yourself for sever headaches in the future when running your application in different configurations.

Method 3

copy the image in your project root folder and provide the image source as only the name of the image instead of the complete path.
e.g., img src = “battlefield.jpg”


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x