I recently wrote a piece of code like this:
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);
}
}
}
}
It pathed correctly but it didn't work because it was pulling off the c:// drive so I changed the location of the image to: http://www.companysite.com/img/XL/
instead. When I replace string PathToFolder = "C://LiveSite/img/XL/";
with http://www.companysite.com/img/XL/
I get the error "URI formats are not supported"
So then I changed my code to:
protected void OpenImg_Click(object sender, EventArgs e)
{
int i = 0;
string uriPath = "http://www.companysite.com/img/XL/";
string localPath = new Uri(uriPath).LocalPath;
string FileName = Variables.param + "XL.jpg";
var foundFiles = localPath.GetFiles(FileName); //ERROR
if (foundFiles.Length == 1)
{
ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + uriPath + foundFiles[i].Name + "');", true);
}
}
}
}
Now I get an error from using .GetFiles - What am I suppose to use instead of GetFiles? and is my code correct to pull the image from the web? Any help would be appreciated.
I was having the same problem.
What I noticed is that it provides me with the file path of the assembly, but with "file:///" at the start of the string.
So I did this:
Hope this helps.
You need to use the webclient class http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx.
This should help you out:
code borrowed from:
http://www.fryan0911.com/2009/03/c-download-file-from-website-using-web.html
It looks like you are using files on your own server, then you can grab them like this from codebehind/controller.