Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the syntax for ImageURL from another in-house server? 1

Status
Not open for further replies.

AndyInNC

Programmer
Sep 22, 2008
76
US
This does not seem like it should be that hard, but for some reason I'm stuck.

We have a file server that houses images from several applications and our web server needs to display them. When I add this code, I get a broken image icon.

HTML:
<asp:Image ID="imgPhoto" runat="server" Width="140px"
    ImageUrl="\\SERVERNAME\FolderName\44959\Graphic.jpg" />

Nothing is being uploaded, so no virtual path is necessary.

The code is on the development PC which is in the same domain as the web server. Am I missing something obvious?

Thanks!
 
Thanks jbenson001. What you said makes perfect sense, but I couldn't make it happen. It turns out what I thought was just a file server for the images does more than that so they won't let me touch IIS there.

Here's the approach I took. Hopefully someone can use it.

As you said, it has to be a URL, so I made it one by creating ImageViewer.aspx which only has a DIV.

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Then I added this to the code behind:

Code:
protected void Page_Load(object sender, System.EventArgs e)
{
    if (Request.QueryString("sFileName") + "" > "") {
        try {
            [COLOR=#4E9A06]// Read the file and convert it to Byte Array[/color]
            string sFileName = Request.QueryString("sFileName");
            string sDirectory = Request.QueryString("sDirectory");

            [COLOR=#4E9A06]// SourcePath is from web.config in the format of \\ServerName\Folder1\Folder
            // In my code, sDirectory is dynamically built, but I've modified it here to act
            // like it's passed in in the QueryString.[/color]
            string sFilePath = Path.Combine(ConfigurationManager.AppSettings.Item("SourcePath"), sDirectory);

            [COLOR=#4E9A06]// Used for generating the image in the viewer.[/color]
            string sContentType = "image/" + Path.GetExtension(sFileName).Replace(".", "");

            FileStream fs = new FileStream(sFilePath + sFileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes(Convert.ToInt32(fs.Length));

            br.Close();
            fs.Close();

            [COLOR=#4E9A06]//Write the file to Reponse[/color]
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = sContentType;
            Response.AddHeader("content-disposition", "attachment;filename=" + sFileName);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        } catch (Exception ex) {
            throw ex;
        }
    }
}

Now I am able to use the following as the image tag:

HTML:
                    <asp:Image runat='server' ID='imgFile' ImageUrl="./ImageViewer.aspx?sFileName=317179.jpg&sDirectory=ImageFolder\"
                        AlternateText='' Width='115px' ToolTip="317179.jpg" />
 
Good idea. You can take this one step further by using a handler (.ashx). It is a page without the UI. So you have all the same coding abilities without the extra overhead of a UI, which, in your case, you don't need. Take a look into it. It's a good thing to know, and we use it often, especially in the case of getting images from our image server.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top