[英]How to get list of files from web URL outside my web application
我使用ASP.NET 4.5
与C#
。
我创建了一个web API
, 指向 我的Web应用程序中的文件夹以获取一些共享资源。 现在,我想从web-api调用指向的目录中检索文件列表 。
例如,我的Web应用程序驻留在我的本地硬盘驱动器的F:\\Projects\\Web1
目录中。 我在C:\\SharedRes\\images
目录中放置了一些图像。 我创建了一个web-api
,从这个共享目录中获取图像。 例如, http://localhost/api/images/a.jpg
将从C:\\SharedRes\\images
目录获取a.jpg
。 它运行正常,但每当我调用web-api
如http://localhost/api/images
,我想从C:\\SharedRes\\images
获取文件列表 。
我试过以下:
DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("http://localhost/api/images"));
FileInfo[] file = directoryInfo.GetFiles().Where(f => f.Extension == (".bmp") || f.Extension == (".jpg") || f.Extension == (".png") || f.Extension == (".TIFF") || f.Extension == (".gif")).ToArray();
但它给出了URI format not supported
错误。
如果有任何解决方案,请告诉我。
HttpServerUtility.MapPath方法将虚拟路径作为参数。
你应该使用HttpContext.Current.Server.MapPath("/api/images")
您只需使用Directory
类及其GetFiles
方法即可。 以下代码用于获取C:\\SharedRes\\images\\
目录中的所有文件:
string[] _Files = Directory.GetFiles(@"C:\SharedRes\images\");
if (_Files != null && _Files.Count() > 0)
{
FileInfo _file;
string _fileName="";
foreach (string file in _Files)
{
_file = new FileInfo(file);
_fileName = _fileName + @"<br/>" + _file.Name;
}
Response.Write(_fileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.