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!
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.
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
Check the permissions on the folder on the server. You may have copied it over from your dev machine with permissions unique to your computer that limit others' access.
The problem with your image is that you are referring to a location on YOUR computer, instead of a URL relative to the location of the file itself. For the browser to be able to render the image, it must be able to get to that image's URL. What you are effectively saying to the browser is to find the image on the users computer, instead on the server, or elsewhere
Thanks to everyone for your ideas.
Got there by adding the SERVERNAME\IUSER_SERVERNAME user with read/write permissions to wwwroot/Images
Then using Server.MapPath("~/images") to save the images to the directory on the server
Then simply Rendering the images from ("~/Images") + CandidatePhoto as I was before.
Thanks again!
You can't put the path "c:\images", because when the image will be rendered the browser will looking for the image at client machine. You can do one of these two solutions:
You can create the directory "/images" at your website and set the site to "impersonate" some user, and set the permissions for that user to allow him to write to this folder. And at the imageUrl you will put something like this: "/images/image01.jpg".
You can create a page that will render the image for you, and pass an ID of the image by post or querystring. Something like "ImageRender.aspx?ID=123". And the page will load the image from "c:\images" and write the image at the response of the page. And At you ImageURL you will put always "ImageRender.aspx?ID=[imageID]".
I hope this helps.
精彩评论