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


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

    以下是具體代碼

 

 

  1 using System;
2 using System.Configuration;
3 using System.Data;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 using System.IO;
13 using System.Net;
14 using System.Text;
15
16 public partial class _Default : System.Web.UI.Page
17 {
18 //以下字段配置在web.config
19 private string ftpServerIP = "127.0.0.1";//服務器ip
20 private string ftpUserID = "FTPTEST";//用戶名FTPTEST
21 private string ftpPassword = "ftptest";//密碼
22 protected void Page_Load(object sender, EventArgs e)
23 {
24
25 if (MyFile.Value != "")
26 {
27 //string a = MyFile.;
28 }
29
30 }
31
32
33
34
35
36
37
38
39 //ftp的上傳功能
40 private void Upload(string filename)
41 {
42 FileInfo fileInf = new FileInfo(filename);
43
44 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
45 FtpWebRequest reqFTP;
46
47 // 根據uri創建FtpWebRequest對象
48 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
49
50 // ftp用戶名和密碼
51 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
52
53 // 默認為true,連接不會被關閉
54 // 在一個命令之后被執行
55 reqFTP.KeepAlive = false;
56
57 // 指定執行什么命令
58 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
59
60 // 指定數據傳輸類型
61 reqFTP.UseBinary = true;
62
63 // 上傳文件時通知服務器文件的大小
64 reqFTP.ContentLength = fileInf.Length;
65
66 // 緩沖大小設置為2kb
67 int buffLength = 2048;
68
69 byte[] buff = new byte[buffLength];
70 int contentLen;
71
72 // 打開一個文件流 (System.IO.FileStream) 去讀上傳的文件
73 FileStream fs = fileInf.OpenRead();
74 try
75 {
76 // 把上傳的文件寫入流
77 Stream strm = reqFTP.GetRequestStream();
78
79 // 每次讀文件流的2kb
80 contentLen = fs.Read(buff, 0, buffLength);
81
82 // 流內容沒有結束
83 while (contentLen != 0)
84 {
85 // 把內容從file stream 寫入 upload stream
86 strm.Write(buff, 0, contentLen);
87
88 contentLen = fs.Read(buff, 0, buffLength);
89 }
90
91 // 關閉兩個流
92 strm.Close();
93 fs.Close();
94 this.Page.RegisterStartupScript("", "<script>alert('成功')</script>");
95 }
96 catch (Exception ex)
97 {
98 // MessageBox.Show(ex.Message, "Upload Error");
99 Response.Write("Upload Error:" + ex.Message);
100 }
101 }
102
103
104 //從ftp服務器上下載文件的功能
105 private void Download(string filePath, string fileName)
106 {
107 FtpWebRequest reqFTP;
108
109 try
110 {
111 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
112
113 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
114
115 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
116
117 reqFTP.UseBinary = true;
118
119 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
120
121 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
122
123 Stream ftpStream = response.GetResponseStream();
124
125 long cl = response.ContentLength;
126
127 int bufferSize = 2048;
128
129 int readCount;
130
131 byte[] buffer = new byte[bufferSize];
132
133 readCount = ftpStream.Read(buffer, 0, bufferSize);
134
135 while (readCount > 0)
136 {
137 outputStream.Write(buffer, 0, readCount);
138
139 readCount = ftpStream.Read(buffer, 0, bufferSize);
140 }
141
142 ftpStream.Close();
143
144 outputStream.Close();
145
146 response.Close();
147 }
148 catch (Exception ex)
149 {
150 Response.Write("Download Error:" + ex.Message);
151 }
152 }
153
154 //從ftp服務器上獲得文件列表
155 public string[] GetFileList()
156 {
157 string[] downloadFiles;
158 StringBuilder result = new StringBuilder();
159 FtpWebRequest reqFTP;
160 // HttpWebRequest reqFTP;
161 try
162 {
163 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
164 reqFTP.UseBinary = true;
165 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
166 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
167 WebResponse response = reqFTP.GetResponse();
168 StreamReader reader = new StreamReader(response.GetResponseStream());
169 string line = reader.ReadLine();
170 while (line != null)
171 {
172 result.Append(line);
173 result.Append("\n");
174 line = reader.ReadLine();
175 }
176 // to remove the trailing '\n'
177 result.Remove(result.ToString().LastIndexOf('\n'), 1);
178 reader.Close();
179 response.Close();
180 return result.ToString().Split('\n');
181 }
182 catch (Exception ex)
183 {
184 downloadFiles = null;
185 return downloadFiles;
186 }
187 }
188
189 protected void Button1_Click(object sender, EventArgs e)
190 {
191 Upload("F:\\美國隊長DVD中字.rmvb");
192 }
193 protected void Button2_Click(object sender, EventArgs e)
194 {
195
196 }
197 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM