Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images not resolving in asp webapp (c#)

I've been working on an asp web application, which involves the user registering details of a person, including an image of the person. The file name of the details/image are stored in a SQL database with the image filename storied in an NVARCHAR column, rather than storing the actual image in the DB.

I created a directory C:\Images to which the image files would be stored by the application. The application works correctly in as far as it moves the images to this location, but when I open the page which would display the details/image the image never renders.

I got round this during development by having the image stored in a folder which was part of the project, but after release of the project to the server, the application refuses to allow the image to be saved in any directories within C:\Inetpub\wwwroot\ .

So I need find out why the images won't render when they are stored in folders not within the project.

I've checked the source of the HTML page which also points to the correct location and file name:

img id="ctl00_MainContent_CandidateImage" src="C:\Images\applicant11.jpg" alt="Candidate Image" style="border-width:1px;border-style:solid;height:208px;width:208px;" 

The code behind page renders the image in the code below:

// CandidatePhoto filename retrieved from DB.

CandidateImage.ImageUrl = "C:\\Images\\" + CandidatePhoto;

Does anyone have any ideas?

Cheers!

like image 285
gordonk Avatar asked Mar 22 '10 13:03

gordonk


2 Answers

When this renders on the browser, you're effectively telling it to look on the c: drive of the client for the image to display...

I don't know why you can't save with the wwwroot, this is probably just a permissions problem, and could be overcome, or you could create a virtual directory within your site that points at you images folder and use this virtual directory URL in your page.

like image 62
Paddy Avatar answered Sep 29 '22 22:09

Paddy


Your images should be something like /images/bob.jpg relative to the application, not an OS drive, even if this is a virtual path...but the images won't be on the C:\ drive of the client in any case.

If you had a virtual directory Images/ in the IIS Application/Site, and it pointed to C:\Images on the server, your urls would look like this:

CandidateImage.ImageUrl = "~/Images/" + CandidatePhoto;

To create a virtual directory like this, see here

like image 24
Nick Craver Avatar answered Sep 29 '22 22:09

Nick Craver