最后50分求高手解救:C# 2.0 ftp上传下载,遇到(基础连接已经关闭: 接收时发生错误。) 解决了就给分呀!
求高手解救:C# 2.0 ftp上传下载,遇到(基础连接已经关闭: 接收时发生错误。) 为什么有的人用这个程序可以,我用就不行,是跟什么有关呢?
调试在这个地方报错:
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
有需要,我把程序发给你们,帮我调一下!万分感激呀!
上传下载都一样.
错误如下:
基础连接已经关闭: 接收时发生错误。
在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
在 System.Net.FtpWebRequest.RequestCallback(Object obj)
在 System.Net.CommandStream.Abort(Exception e)
在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
在 System.Net.FtpWebRequest.GetRequestStream()
在 FTP24CP.Form1.Upload(String filename) 位置 C:\Documents and Settings\Administrator\桌面\ftpTemp\FTP24CP\Form1.cs:行号 80
相关代码如下:
private void btnUpload_Click(object sender, EventArgs e)
{
OpenFileDialog opFilDlg = new OpenFileDialog();
if (opFilDlg.ShowDialog() == DialogResult.OK)
{
Upload(opFilDlg.FileName);
}
}
/// <summary>
/// Method to upload the specified file to the specified FTP Server
/// </summary>
/// <param name="filename">file full name to be uploaded</param>
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}