我在 ftp 服務器上有太多檔案,我想在資料 gridview 中顯示而不保存或下載并按日期排序,我不知道該怎么做。這些檔案是 .qrt 但我可以在 Excel 或記事本中打開它們。我這樣做了,但是從 sql db 但從未使用過 ftp,這些檔案的名稱帶有日期。
我在 C# 中使用 asp.net web 表單。
任何想法我怎么能做到這一點。非常感謝提前
uj5u.com熱心網友回復:
如上所述,這很容易。但是,您沒有提到您是否有一堆要顯示的檔案夾,或者只是一個 FTP 檔案夾中的檔案?
如前所述,如果您有一堆檔案夾,那么使用樹視圖來顯示此層次結構型別設定非常容易。但是,對于一個檔案夾?當然,GridView 是一個不錯的選擇。
你可以說使用這個:
我們的標記是這樣的:
<asp:GridView ID="GridView1" runat="server"
CssClass="table" Width="20%"></asp:GridView>
好的,我們的代碼是這樣的:
using System.Net;
using System.IO;
因此,對于頁面加載,我們有:
const string UserId = "User Name here";
const string Password = "Password here";
const string fRoot = "ftp://ftp.mycoolwebsite.com/test";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
DataTable MyFiles = new DataTable();
MyFiles.Columns.Add("File", typeof(string));
List<string> fList = GetFtpFiles();
foreach (string sFile in fList)
{
DataRow OneRow = MyFiles.NewRow();
OneRow["File"] = sFile;
MyFiles.Rows.Add(OneRow);
}
GridView1.DataSource = MyFiles;
GridView1.DataBind();
}
List<string> GetFtpFiles()
{
FtpWebRequest ftpR = (FtpWebRequest)WebRequest.Create(fRoot);
ftpR.Credentials = new NetworkCredential(UserId, Password);
ftpR.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = ftpR.GetResponse() as FtpWebResponse;
StreamReader sread = new StreamReader(response.GetResponseStream());
List<string> myDir = new List<string>();
string oneLine = sread.ReadLine();
while (!string.IsNullOrEmpty(oneLine))
{
myDir.Add(oneLine);
oneLine = sread.ReadLine();
}
sread.Close();
return myDir;
}
現在輸出是這樣的:

現在,如果我們想要檔案大小?這是一個問題,因為理論上一旦我們有了檔案串列,我們就可以撰寫代碼來“請求”每個檔案大小——這很痛苦,但更糟糕的是有很多請求。另一種方式,是我們可以改變這一點:
ftpR.Method = WebRequestMethods.Ftp.ListDirectory;
到:
ftpR.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
The above will return file information VERY much like an old style DOS file listing.
When we run above with above, we now get this:

So, if we want to sort say by file name, we have to parse out the file name, and date. This is not too hard. As noted, the other way is to "request" the file size an date for each file - but that is extra code, but WORSE is a LOT of hits to the ftp server.
And the style of the file listings are not consistent from web servers (might be windows, or linux), but we COULD parse out the above into columns - and then we can sort by date, or file name.
Parsing? Gee, this works:
void LoadGrid()
{
DataTable MyFiles = new DataTable();
MyFiles.Columns.Add("Date", typeof(DateTime));
MyFiles.Columns.Add("Size", typeof(int));
MyFiles.Columns.Add("File", typeof(string));
List<string> fList = GetFtpFiles();
foreach (string s in fList)
{
DataRow OneRow = MyFiles.NewRow();
string sRow = s;
while (sRow.Contains(" "))
sRow = sRow.Replace(" ", " ");
sRow = sRow.Replace(" ", ",");
string[] scols = sRow.Split(',');
OneRow["Size"] = scols[4];
OneRow["File"] = scols[8];
OneRow["Date"] = scols[7] "-" scols[6] "-" scols[5];
MyFiles.Rows.Add(OneRow);
}
GridView1.DataSource = MyFiles;
GridView1.DataBind();
}
And now we get this:

We need to formt that ate in the grid - but at least now, with a table, you can sort that table by date, or filename before you bind it, say like this:
MyFiles.DefaultView.Sort = "File";
GridView1.DataSource = MyFiles;
GridView1.DataBind();
您也可以/可以按日期排序 - 因為我們將日期列定義為日期時間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/383890.html
