Solved

How to Set the Src Attribute of the Img Tag

Posted on 2011-02-12
4
1,167 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 49
Regex validation 2 24
Active Directory uninstall Windows 2008 R2 6 84
ColdFusion 9 CF Administrator and Request Filtering 2 5
A short article about problems I had with the new location API and permissions in Marshmallow
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

772 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