C#2.0中增加了對FTP操作的支援。
我根據這個介紹,将其轉化到網頁中來,進而完成我們現在項目的需求。
在這個項目中,我使用了三種方法:List ,upload還是download。
背景代碼:
[轉]asp.net (網頁中操作FTP) using System;
[轉]asp.net (網頁中操作FTP) using System.Data;
[轉]asp.net (網頁中操作FTP) using System.Configuration;
[轉]asp.net (網頁中操作FTP) using System.Web;
[轉]asp.net (網頁中操作FTP) using System.Web.Security;
[轉]asp.net (網頁中操作FTP) using System.Web.UI;
[轉]asp.net (網頁中操作FTP) using System.Web.UI.WebControls;
[轉]asp.net (網頁中操作FTP) using System.Web.UI.WebControls.WebParts;
[轉]asp.net (網頁中操作FTP) using System.Web.UI.HtmlControls;
[轉]asp.net (網頁中操作FTP) using System.IO;
[轉]asp.net (網頁中操作FTP) using System.Net;
[轉]asp.net (網頁中操作FTP) using System.Text;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) public partial class _Default : System.Web.UI.Page
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) {
[轉]asp.net (網頁中操作FTP) string ftpServerIP = "59.125.163.103";
[轉]asp.net (網頁中操作FTP) string ftpUserID = "vtek";
[轉]asp.net (網頁中操作FTP) string ftpPassword = "vtek_admin";
[轉]asp.net (網頁中操作FTP) protected void Page_Load(object sender, EventArgs e)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) }
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) private void Upload(string filename)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) FileInfo fileInf = new FileInfo(filename);
[轉]asp.net (網頁中操作FTP) string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
[轉]asp.net (網頁中操作FTP) FtpWebRequest reqFTP;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 根據uri建立FtpWebRequest對象
[轉]asp.net (網頁中操作FTP) reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // ftp使用者名和密碼
[轉]asp.net (網頁中操作FTP) reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 預設為true,連接配接不會被關閉
[轉]asp.net (網頁中操作FTP) // 在一個指令之後被執行
[轉]asp.net (網頁中操作FTP) reqFTP.KeepAlive = false;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 指定執行什麼指令
[轉]asp.net (網頁中操作FTP) reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 指定資料傳輸類型
[轉]asp.net (網頁中操作FTP) reqFTP.UseBinary = true;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 上傳檔案時通知伺服器檔案的大小
[轉]asp.net (網頁中操作FTP) reqFTP.ContentLength = fileInf.Length;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 緩沖大小設定為2kb
[轉]asp.net (網頁中操作FTP) int buffLength = 2048;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) byte[] buff = new byte[buffLength];
[轉]asp.net (網頁中操作FTP) int contentLen;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 打開一個檔案流 (System.IO.FileStream) 去讀上傳的檔案
[轉]asp.net (網頁中操作FTP) FileStream fs = fileInf.OpenRead();
[轉]asp.net (網頁中操作FTP) try
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 把上傳的檔案寫入流
[轉]asp.net (網頁中操作FTP) Stream strm = reqFTP.GetRequestStream();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 每次讀檔案流的2kb
[轉]asp.net (網頁中操作FTP) contentLen = fs.Read(buff, 0, buffLength);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 流内容沒有結束
[轉]asp.net (網頁中操作FTP) while (contentLen != 0)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 把内容從file stream 寫入 upload stream
[轉]asp.net (網頁中操作FTP) strm.Write(buff, 0, contentLen);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) contentLen = fs.Read(buff, 0, buffLength);
[轉]asp.net (網頁中操作FTP) }
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // 關閉兩個流
[轉]asp.net (網頁中操作FTP) strm.Close();
[轉]asp.net (網頁中操作FTP) fs.Close();
[轉]asp.net (網頁中操作FTP) }
[轉]asp.net (網頁中操作FTP) catch (Exception ex)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) //MessageBox.Show(ex.Message, "Upload Error");
[轉]asp.net (網頁中操作FTP) Response.Write(ex.Message);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) private void Download(string filePath, string fileName)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) //filePath = <<The full path where the file is to be created. the>>,
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) //fileName = <<Name of the file to be createdNeed not name on FTP server. name name()>>
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) reqFTP.UseBinary = true;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) Stream ftpStream = response.GetResponseStream();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) long cl = response.ContentLength;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) int bufferSize = 2048;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) int readCount;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) byte[] buffer = new byte[bufferSize];
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) readCount = ftpStream.Read(buffer, 0, bufferSize);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) while (readCount > 0)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) outputStream.Write(buffer, 0, readCount);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) readCount = ftpStream.Read(buffer, 0, bufferSize);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) ftpStream.Close();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) outputStream.Close();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) response.Close();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) Response.Write(ex.Message);
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) public string[] GetFileList()
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) string[] downloadFiles = new string[100];
[轉]asp.net (網頁中操作FTP) StringBuilder result = new StringBuilder();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
[轉]asp.net (網頁中操作FTP) WebResponse response = reqFTP.GetResponse();
[轉]asp.net (網頁中操作FTP) StreamReader reader = new StreamReader(response.GetResponseStream());
[轉]asp.net (網頁中操作FTP) string line = reader.ReadLine();
[轉]asp.net (網頁中操作FTP) while (line != null)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) result.Append(line);
[轉]asp.net (網頁中操作FTP) result.Append("\n");
[轉]asp.net (網頁中操作FTP) line = reader.ReadLine();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) // to remove the trailing '\n'
[轉]asp.net (網頁中操作FTP) result.Remove(result.ToString().LastIndexOf('\n'), 1);
[轉]asp.net (網頁中操作FTP) reader.Close();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) return result.ToString().Split('\n');
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) downloadFiles = null;
[轉]asp.net (網頁中操作FTP) return downloadFiles;
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) protected void Button1_Click(object sender, EventArgs e)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) //string[] result=this.GetFileList();
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) //if (result.Length > 0)
[轉]asp.net (網頁中操作FTP) // foreach (string s in result)
[轉]asp.net (網頁中操作FTP) // {
[轉]asp.net (網頁中操作FTP) // Response.Write(s + "<br/>");
[轉]asp.net (網頁中操作FTP) // }
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) //this.Download("d:\\", "sp.sql");
[轉]asp.net (網頁中操作FTP) this.Upload("d:\\shipfa");
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) }
[轉]asp.net (網頁中操作FTP) 前台代碼:
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) <%
[轉]asp.net (網頁中操作FTP) @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
[轉]asp.net (網頁中操作FTP)
[轉]asp.net (網頁中操作FTP) <html xmlns="http://www.w3.org/1999/xhtml" >
[轉]asp.net (網頁中操作FTP) <head runat="server">
[轉]asp.net (網頁中操作FTP) <title>無标題頁</title>
[轉]asp.net (網頁中操作FTP) </head>
[轉]asp.net (網頁中操作FTP) <body>
[轉]asp.net (網頁中操作FTP) <form id="form1" runat="server">
[轉]asp.net (網頁中操作FTP) <div>
[轉]asp.net (網頁中操作FTP) <asp:Button ID="Button1" runat="server" Text="List" OnClick="Button1_Click" />
[轉]asp.net (網頁中操作FTP) </div>
[轉]asp.net (網頁中操作FTP) </form>
[轉]asp.net (網頁中操作FTP) </body>
[轉]asp.net (網頁中操作FTP) </html>
源碼下載下傳:
/Files/xxpyeippx/WebSite2.rar
Surance Yin@ Surance Center