[英]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.