簡體   English   中英

如何從Web應用程序外部的Web URL獲取文件列表

[英]How to get list of files from web URL outside my web application

我使用ASP.NET 4.5C#

我創建了一個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-apihttp://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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM