C# .NET - how to show image from local drive D

Asked By pankaj singh on 21-Sep-11 02:07 PM
Hi expert
     Is it possible to show image of my local drive from D drive in web site in asp.net
Devil Scorpio replied to pankaj singh on 21-Sep-11 03:15 PM
Hi,

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 Files\Adrenalin\Adrenalin\UploadedFiles\TemplateFile 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 Files\Adrenalin\Adrenalin\UploadedFiles\TemplateFile\abc.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" />
Riley K replied to pankaj singh on 21-Sep-11 10:15 PM
 you could do something like this:

Your main page:

protected void Page_Load(object sender, EventArgs e)
{
  mainImage.ImageUrl = "ImageHandler.ashx?image=MyImage.jpg";
}

ImageHandler:

public void ProcessRequest(HttpContext context)
{
  byte[] imageBytes = File.ReadAllBytes(@"C:\Images" + context.Request["image"]);
  context.Response.ContentType = "image/jpeg";
  context.Response.BinaryWrite(imageBytes);
}


Regards
TSN ... replied to pankaj singh on 21-Sep-11 11:42 PM
hi..


App_Data

Contains application data files including .mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information. For more information, see http://msdn.microsoft.com/en-us/library/yh26yfzy.aspx and http://msdn.microsoft.com/en-us/library/5k850zwb.aspx.



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 Files\Adrenalin\Adrenalin\UploadedFiles\TemplateFile\abc.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" />
Jitendra Faye replied to pankaj singh on 21-Sep-11 11:47 PM
Yes ,it is possible but it is must that ,

image file should be in your local project location.

otherwise cross mark will be shown in image control.
pankaj singh replied to Devil Scorpio on 22-Sep-11 12:21 AM
hi Devil Scorpio
   Thank a lot. But It show only one image on whole page ,but i have different images in same page, then how i show on fix loaction