嗨如何在我的網站上顯示文件以下載它。我有代碼:在我的網站上顯示文件
Directory.GetFiles("http://example.com/Folder1/Folder2", "*.*")
但它不工作。我可以這樣使用它:
Directory.GetFiles(@"C:\Program Files\Folder1\Folder2", "*.*")
我該如何使用此代碼來顯示http://example.com/Folder1/Folder2中的文件?
嗨如何在我的網站上顯示文件以下載它。我有代碼:在我的網站上顯示文件
Directory.GetFiles("http://example.com/Folder1/Folder2", "*.*")
但它不工作。我可以這樣使用它:
Directory.GetFiles(@"C:\Program Files\Folder1\Folder2", "*.*")
我該如何使用此代碼來顯示http://example.com/Folder1/Folder2中的文件?
謝謝大家的答案。我爲C#下載了ftp客戶端。我使用的代碼:
ftp.Connect("ftp.domain.com");
ftp.Login("user", "pw");
// If files in : domains/httpdocs/Install/Program
ftp.ChangeFolder("domains");
ftp.ChangeFolder("httpdocs");
ftp.ChangeFolder("Install");
ftp.DownloadFiles("Program",
"C:/Program Files/Install/", new RemoteSearchOptions("*.*", true));
ftp.Close();
你可以從這裏下載的FTP客戶端:http://www.limilabs.com/ftp
來自文檔:*返回指定的目錄中的文件名(包括它們的路徑)。
你正在向它傳遞一個URL到一個網站,所以這就是爲什麼它沒有返回任何東西 - 它期待着一個本地路徑。
感謝您的回答,我可以使用ftp – WalkerJetBat 2012-07-27 18:52:58
在一個體面的網站上沒有辦法做到這一點,如果你知道它是URL,你可以下載一個文件,但是如果網站設置正確,沒有辦法直接獲取所有文件或獲取目錄結構。
從Web服務器下載文件,你必須使用WebClient,像這樣:
WebClient wc = new WebClient();
wc.DownloadFile(" http://example.com/Folder1/Folder2/File.txt", "C:\\temp\\File.txt");
從FTP服務器上下載文件,使用FtpWebRequest,這裏是列出目錄中的文件的例子:
http://www.coding.defenselife.com/index.php/articles/20-ftpwebrequest-sample-c
下載文件感謝您的回答 – WalkerJetBat 2012-07-27 18:53:53
你的問題有點含糊,但我想我找到了你要找的東西。無論如何,我會假設你使用ASP.NET,第一步是創建一些東西來顯示你的文件。我已經使用了一個只有超鏈接的中繼器,如下所示:
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="hyp" runat="server" />
</ItemTemplate>
</asp:Repeater>
之後,您需要填寫中繼器。你可以做,在這樣的頁面加載:
if (!Page.IsPostBack)
{
string[] files = Directory.GetFiles(@"C:\testfolder");
rpt.DataSource = files;
rpt.DataBind();
}
下一步你能做的就是完整喜歡的ItemDataBound方法,以便:
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string file = e.Item.DataItem as string;
HyperLink hyp = e.Item.FindControl("hyp") as HyperLink;
hyp.Text = file;
hyp.NavigateUrl = string.Format("~/Handlers/FileHandler.ashx?file={0}", file);
}
}
正如你可以在導航網址看到,我們要去使用HttpHandler。當你創建一個新的Handler文件(.ashx)時。在其ProcessRequest方法,你需要這樣的事情,所以該文件是avaible下載:
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Request.QueryString["file"]);
context.Response.WriteFile(context.Request.QueryString["file"]);
context.Response.End();
}
不要忘記註冊您的處理程序在web.config中的System.Web節點像這樣:
<httpHandlers>
<add verb="*" path="~/Handlers/FileHandler.ashx?file={0}" type="StackOverflow.Questions.Handlers.FileHandler, StackOverflow.Questions"/>
</httpHandlers>
請記住,像我這樣做的查詢字符串傳遞路徑不應該做,但我不知道你的應用程序如何工作,所以找到適合你的東西。
祝你好運!
你應該看看這個: http://stackoverflow.com/questions/124492/c-sharp- httpwebrequest-command-to-get-directory-listing – Charlie 2012-07-27 18:56:39