asp.net實現ftp上傳代碼(解決大文件上傳問題)

fmms 12年前發布 | 78K 次閱讀 .NET開發 ASP.NET

   原來使用asp.net上傳控件上傳 那個雖然簡單但是頁面不是很友好 然后就用了uploadify上傳控件  這個控件雖然界面友好 但是大文件還是不能上傳 而且在不同的瀏覽器會出現session丟失問題 所以我到了個ftp上傳的方法

    以下是具體代碼

using System;
 using System.Configuration;
 using System.Data;
 using System.Linq;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.HtmlControls;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Xml.Linq;
 using System.IO;
 using System.Net;
 using System.Text;

public partial class _Default : System.Web.UI.Page { //以下字段配置在web.config private string ftpServerIP = "127.0.0.1";//服務器ip private string ftpUserID = "FTPTEST";//用戶名FTPTEST private string ftpPassword = "ftptest";//密碼 protected void Page_Load(object sender, EventArgs e) {

     if (MyFile.Value != "")
     {
         //string a = MyFile.;
     }

 }








 //ftp的上傳功能
 private void Upload(string filename)
 {
     FileInfo fileInf = new FileInfo(filename);

     string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
     FtpWebRequest reqFTP;

     // 根據uri創建FtpWebRequest對象 
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

     // ftp用戶名和密碼
     reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

     // 默認為true,連接不會被關閉

// 在一個命令之后被執行 reqFTP.KeepAlive = false;

     // 指定執行什么命令
     reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

     // 指定數據傳輸類型
     reqFTP.UseBinary = true;

     // 上傳文件時通知服務器文件的大小
     reqFTP.ContentLength = fileInf.Length;

     // 緩沖大小設置為2kb
     int buffLength = 2048;

     byte[] buff = new byte[buffLength];
     int contentLen;

     // 打開一個文件流 (System.IO.FileStream) 去讀上傳的文件
     FileStream fs = fileInf.OpenRead();
     try
     {
         // 把上傳的文件寫入流
         Stream strm = reqFTP.GetRequestStream();

         // 每次讀文件流的2kb
         contentLen = fs.Read(buff, 0, buffLength);

         // 流內容沒有結束
         while (contentLen != 0)
         {
             // 把內容從file stream 寫入 upload stream
             strm.Write(buff, 0, contentLen);

             contentLen = fs.Read(buff, 0, buffLength);
         }

         // 關閉兩個流
         strm.Close();
         fs.Close();
         this.Page.RegisterStartupScript("", "<script>alert('成功')</script>");
     }
     catch (Exception ex)
     {
         // MessageBox.Show(ex.Message, "Upload Error");
         Response.Write("Upload Error:" + ex.Message);
     }
 }


 //從ftp服務器上下載文件的功能
 private void Download(string filePath, string fileName)
 {
     FtpWebRequest reqFTP;

     try
     {
         FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));

         reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

         reqFTP.UseBinary = true;

         reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

         FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

         Stream ftpStream = response.GetResponseStream();

         long cl = response.ContentLength;

         int bufferSize = 2048;

         int readCount;

         byte[] buffer = new byte[bufferSize];

         readCount = ftpStream.Read(buffer, 0, bufferSize);

         while (readCount > 0)
         {
             outputStream.Write(buffer, 0, readCount);

             readCount = ftpStream.Read(buffer, 0, bufferSize);
         }

         ftpStream.Close();

         outputStream.Close();

         response.Close();
     }
     catch (Exception ex)
     {
         Response.Write("Download Error:" + ex.Message);
     }
 }

 //從ftp服務器上獲得文件列表
 public string[] GetFileList()
 {
     string[] downloadFiles;
     StringBuilder result = new StringBuilder();
     FtpWebRequest reqFTP;
     // HttpWebRequest reqFTP;
     try
     {
         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
         reqFTP.UseBinary = true;
         reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
         reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
         WebResponse response = reqFTP.GetResponse();
         StreamReader reader = new StreamReader(response.GetResponseStream());
         string line = reader.ReadLine();
         while (line != null)
         {
             result.Append(line);
             result.Append("\n");
             line = reader.ReadLine();
         }
         // to remove the trailing '\n'        
         result.Remove(result.ToString().LastIndexOf('\n'), 1);
         reader.Close();
         response.Close();
         return result.ToString().Split('\n');
     }
     catch (Exception ex)
     {
         downloadFiles = null;
         return downloadFiles;
     }
 }

 protected void Button1_Click(object sender, EventArgs e)
 {
     Upload("F:\\美國隊長DVD中字.rmvb");
 }
 protected void Button2_Click(object sender, EventArgs e)
 {

 }

}</pre>

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!