0

Okay after massive amount of Googling and trying different things this is probably something simple that I'm messing up on.

Basically what I'm doing is parsing a productID from a url and adding XL.jpg to the end. For example lets say the product id was 1234 the program would search for 1234XL.jpg in the folder that's provided in the PathToFolder (C://LiveSite/img/XL/).

Everything works as planned until the pathing part. It parses the url adds XL.jpg to the end and even follows the path I have set, but I get an error, "The address wasn't understood. Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any program." The other browsers just have a blank window.

This is what shows up in my browser: c://LiveSite/img/XL/1234XL.jpg

protected void OpenImg_Click(object sender, EventArgs e)
                {
                    int i = 0;

                    string PathToFolder = "C://LiveSite/img/XL/";

                    var dirInfo = new DirectoryInfo(PathToFolder);
                    string FileName = Variables.param + "XL.jpg";
                    var foundFiles = dirInfo.GetFiles(FileName);

                    if (foundFiles.Length == 1)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + PathToFolder + foundFiles[i].Name + "');", true);
                    }
                }
            }
       }

What am I messing up on that it won't path to the server? Is there something I should be using instead of C:// ? I've tried flipping the slashes the other way and using C:/ none worked.

Thanks in advance for your help.

Edit:

Sorry about the confusion. The images are not located on my computer or in the project. They are on a remote server, sql server if that matters. I'm not sure if I am starting the directory off correctly if I want to link to a server.

On the server I want to link it to the path to the folder in C://LiveSite/img/XL/ I now understand that linking it like that would only open if i was doing it on that server. So how do I link to that folder from the internet browser? Can I use the piece of code I have written at all or do I have to do it a completely new way?

This is an asp.net web application

JavaTheScript
  • 137
  • 3
  • 16
  • you haven't tell about your application type. is this asp.net web application? if this is web app where your images located? inside your web site root or sub folder? or another virtual directory? – Damith Jun 07 '13 at 18:25
  • yes it's an asp.net web application. my images are located on a company server. no the images are not located in the project. – JavaTheScript Jun 07 '13 at 18:28
  • can't you access images from company live site? like `www.yourcompany.com/img/XL/1234XL.jpg`? – Damith Jun 07 '13 at 18:32
  • You would think so but the company uses multiple site names and there are thousands of products so it would take me forever to do that. That's why its dynamically done. When a customer chooses a product it has to parse that specific productID and then find the image in the folder on the server of the same name. – JavaTheScript Jun 07 '13 at 18:35
  • if you searching server folders for file it will be more time and resource consuming. can't you create table with product id and matching image paths/urls and take from there on request? you can update this table daily or hourly if your site rapidly changing. – Damith Jun 07 '13 at 18:43
  • @Damith okay I have done the suggestion of having the www.yourcompany.com/img/XL/1234XL.jpg - The problem now is I get an error that says "URI formats are not supported." All I did was replace my PathToFolder with the URL instead. What am I doing wrong? – JavaTheScript Jun 20 '13 at 20:50

2 Answers2

1

If C://LiveSite/ is your website root directory then you can use this

protected void OpenImg_Click(object sender, EventArgs e)
{
    int i = 0;

    string PathToFolder = "C://LiveSite/img/XL/";

    string webRootPathToFolder = ResolveUrl("~/img/XL/");
    var dirInfo = new DirectoryInfo(PathToFolder);
    string FileName = Variables.param + "XL.jpg";
    var foundFiles = dirInfo.GetFiles(FileName);

    if (foundFiles.Length == 1)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + webRootPathToFolder + foundFiles[i].Name + "');", true);
    }
}
Yograj Gupta
  • 9,811
  • 3
  • 29
  • 48
0

Try something like this:

file:///c:/path/to/the%20file.txt

If you have to... opening local files in the browser is not a widely used technique.

It will only work on your computer so to speak. There is no way this logic will work if the browser is not running on the same machine as the website.

You should consider a solution where the server sends the file's content to the browser in a byte[]. There is an example of that here.

Community
  • 1
  • 1
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222