Solved

How to Set the Src Attribute of the Img Tag

Posted on 2011-02-12
4
1,171 Views
Last Modified: 2012-05-11
I have an ASP.NET page that, when viewed, defines an HMTL table with a row, plus cells within the table row using code like the following:

        HtmlTable tbl = new HtmlTable()
        HtmlTableRow tblRow = new HtmlTableRow()
        HtmlTableCell tblCell = new HtmlTableCell()

        Page.Controls.Add(tbl)

The page dynamcally fills the HTML table with 10 images (Photo1.jpg, Photo2.jpg, Photo3.jpg, …, Photo10.jpg) that are stored in a subfolder, (the Images subfolder) under the website’s root folder. The physical path is similar to C:\Inetpub\wwwroot\mysite\images and the InTRAnet site is browsed using a URL like http://Myserver/mysite/mypage.aspx. The ASP.NET page contains a For...Next loop like the following, which populates the HTML table:

    For I = 1 to 10
        tblCell.InnerHtml = "<img src='/Images/Photo" & CStr(I) & ".jpg' />"
        tblRow.Cells.Add(tblCell)
    Next I

This works fine and the HTML table properly displays all 10 images. The path assigned to the src attribute is, of course, the relative path, and I could have used the full path of <img scr=’http://myserver/mysite/images/photo1.jpg’ />, etc., and the page would have rendered the images as well.

The problem I am having is that a new “Pictures” folder has been introduced on the same IIS machine, but outside of the website. Its path is similar to C:\Photos\Pictures. Therefore, using the same full or relative path like that above does not work, since the Pictures folder “lives” outside of the website. So I have tried assigning paths like the following to the src attribute, but without success:

    tblCell.InnerHtml = "<img src='file://C:/Photos/Pictures/Photo" & CStr(I) & ".jpg' />"
or
    tblCell.InnerHtml = "<img src='C:\Photos\Pictures\Photo" & CStr(I) & ".jpg' />"

And I even tried including the literal filename, such as:

    tblCell.InnerHtml = "<img src='C:\Photos\Pictures\Photo1.jpg' />"

I have tried so many combinations like those above that I have lost track. The result is either thumbnails or a red X that is displayed in each cell of the HTML table, instead of displaying actual photos.

I initially thought that the problem  was that maybe the page was processing client-side, instead of server-side, and therefore on the client machine, there would be no such path as C:\Photos\Pictures. But you get the same results (red X or thumbnails) when you browse the ASP page while sitting directly at the IIS machine.

To ensure that it wasn’t a permissions issue, I temporarily assigned to folder C:\Photos\Pictures the permissions of Read & Execute, List Folders, and Read, and these were assigned to group Everyone. That should give IUser (literally all users) sufficient access to the path, but the page still would not display the photos.

Any suggestions on how I can resolve this problem? Unfortunately, moving the Pictures folder inside of the website is not an option.

Thanks
0
Comment
Question by:brian_appliedcpu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34880694
You don't need to physically move the folder underneath your site, but you do need to create a virtual directory to it. The path to the file needs to be relative to your site and cannot directly access a path outside of your site.
0
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 34881387
hi
you can go as @carl_tawn comment
or simply create a folder under ur site and read the images from it
0
 
LVL 11

Expert Comment

by:G_H
ID: 34882156
The best option is a virtual directory. But, just so that you understand what is going on, and why it is failing when you use...

src="C:\pictures...

It is the client machine looking at it's own C drive.

I believe you can use this method, if you use a UNC path to the image file.

GH
0
 
LVL 2

Author Closing Comment

by:brian_appliedcpu
ID: 34882577
Use of a new virtual that points to the "external" path works, so I will use that. Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Having trouble getting Imagick working with php on windows. 2 47
AvlTree-Node Data type 4 22
asp.net figure out error 3 20
Securing WEBAPI on Azure 2 21
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
A phishing scam that claims a recipient’s credit card details have been “suspended” is the latest trend in spoof emails.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question