我最近写了一段这样的代码:
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);
}
}
}
}
它正确地进行了修改,但它没有用,因为它正在关闭c://驱动器,所以我将图像的位置更改为:http://www.companysite.com/img/XL/
。当我用string PathToFolder = "C://LiveSite/img/XL/";
替换http://www.companysite.com/img/XL/
时,我收到错误“不支持URI格式”
然后我将代码更改为:
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);
}
}
}
}
现在我收到使用.GetFiles的错误 - 我想用什么而不是GetFiles?并且我的代码是否正确从网络上提取图像?任何帮助将不胜感激。
答案 0 :(得分:2)
您需要使用webclient类http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx。
这可以帮助你:
using System;
using System.Net;
using Myapp.Properties;
namespace MyApp
{
public class Test
{
static void Main(string[] args)
{
string website ="http://www.fryan0911.com/webclientsample.zip";
string downloadfolder = Settings.Default.DownloadFolder;
try
{
WebClient wc = new WebClient();
wc.DownloadFile(website, downloadfolder);
Console.WriteLine("Web file has been downloaded to " + downloadfolder);
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
代码来自:
http://www.fryan0911.com/2009/03/c-download-file-from-website-using-web.html
答案 1 :(得分:0)
看起来你在自己的服务器上使用文件,然后你可以从codebehind / controller中抓取它们。
var serverRootPath = Server.MapPath("~");
var files = Path.Combine(serverRootPath,"img");
var images = Directory.GetFiles(files);
答案 2 :(得分:0)
我遇到了同样的问题。
string app_path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
string folderpath = Path.GetDirectoryName(app_path);// Error Here
我注意到它为我提供了程序集的文件路径,但是使用" file:///"在字符串的开头。
所以我这样做了:
string app_path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(@"file:///", "");
希望这有帮助。