用c#将文件从本地传输到FTP服务器
本文关键字:传输 FTP 服务器 文件 | 更新日期: 2023-09-27 18:17:34
我想从本地传输文件到ftp(文件传输协议)服务器,通常它与一个小文件一起工作,但如果我有一个大文件,我有这个问题("与服务器的连接被重置,而页面加载")
这是我的代码:
//FTP Server URL.
string ftp = ipFTP;
//FTP Folder name. Leave blank if you want to upload to root folder.
string ftpFolder = dossier_DAO;
byte[] fileBytes = null;
//Read the FileName and convert it to Byte array.
string fileName = Path.GetFileName(FileUpload1.FileName);
using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
{
fileBytes = FileUpload1.FileBytes;
fileStream.Close();
}
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
//Enter FTP Server credentials.
request.Credentials = new NetworkCredential(loginFTP, pwdFTP);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//lblMessage.Text += fileName + " uploaded.<br />";
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
我必须在web配置中添加这段代码:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="900000000000" />
</requestFiltering>
</security>
</system.webServer>