Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

asp.net - 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....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 physicalFileName=@"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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...