I am getting a lot of different issues in this function :
我在这个函数中遇到了很多不同的问题:
public static bool UploadToFTP(string strFileName, string strFolderName)
{
bool isUploaded = false;
string strFilename = string.Empty;
string strFtpURI = string.Empty;
string strFtpUserId = string.Empty;
string strFtpPassword = string.Empty;
byte[] buffer = null;
FileInfo oFileInfo = null;
FileStream oFileStream = null;
FtpWebRequest oFtpWebRequest = null;
try
{
strFilename = strFileName;
oFileInfo = new FileInfo(strFilename);
strFtpURI = Constants.FtpUri;
strFtpUserId = Constants.FtpUserID;
strFtpPassword = Constants.FtpPassword;
oFtpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpURI + "/" + strFolderName + "/" + oFileInfo.Name));
oFtpWebRequest.Credentials = new NetworkCredential(strFtpUserId, strFtpPassword);
oFtpWebRequest.Proxy = null;
oFtpWebRequest.KeepAlive = false;
oFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
oFtpWebRequest.UseBinary = true;
oFtpWebRequest.ContentLength = oFileInfo.Length;
int iBufferLength = 2084;
buffer = new byte[iBufferLength];
int iContentLength = 0;
oFileStream = oFileInfo.OpenRead();
try
{
iContentLength = oFileStream.Read(buffer, 0, iBufferLength);
using (Stream oStream = oFtpWebRequest.GetRequestStream())
{
while (iContentLength != 0)
{
oStream.Write(buffer, 0, iContentLength);
iContentLength = oFileStream.Read(buffer, 0, iBufferLength);
}
isUploaded = true;
FtpUpload.TotalKBFilesUploaded = FtpUpload.TotalKBFilesUploaded + (int)(oFileInfo.Length / 1000);
}
}
catch (Exception ex)
{
}
finally
{
if (oFtpWebRequest != null)
{
oFtpWebRequest.Abort();
oFtpWebRequest = null;
}
if (buffer != null)
{
buffer = null;
}
if (oFileStream != null)
{
oFileStream.Close();
oFileStream.Dispose();
}
}
}
catch (Exception ex)
{
}
finally
{
oFileInfo = null;
}
return isUploaded;
}
This is uploading 1000s of images to FTP, and this methos is called in multi threaded way.
这是将1000个图像上传到FTP,这种方法以多线程方式调用。
Different errors are :
不同的错误是:
=======================================================
Message:The operation has timed out
消息:操作已超时
Error Trace: at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.GetRequestStream()
错误跟踪:位于System.Net.FtpWebRequest.GetRequestStream()的System.Net.FtpWebRequest.CheckError()处
=======================================================
Error Message:Unable to connect to the remote server
错误消息:无法连接到远程服务器
Error Trace: at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.GetRequestStream()
错误跟踪:位于System.Net.FtpWebRequest.GetRequestStream()的System.Net.FtpWebRequest.CheckError()处
=======================================================
Error Message:The underlying connection was closed: An unexpected error occurred on a receive.
错误消息:基础连接已关闭:接收时发生意外错误。
Error Trace: at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.CommandStream.Abort(Exception e) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.GetRequestStream()
错误跟踪:System.Net上的System.Net.FtpWebRequest.CheckError()位于System.Net.FtpWebRequest.FinpRequestStage(RequestStage阶段)的System.Net.CommandStream.Abort(Exception e)上的System.Net.FtpWebRequest.SyncRequestCallback(Object obj)处System.Net.FtpWebRequest.GetRequestStream()
=======================================================
Error Message:Unable to write data to the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
错误消息:无法将数据写入传输连接:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接的主机无法响应而建立的连接失败。
Error Trace: at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.FtpDataStream.Write(Byte[] buffer, Int32 offset, Int32 size) =======================================================
错误跟踪:System.Net.Sockets.NetworkStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)在System.Net.FtpDataStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)===== ==================================================
These are few errors coming from the same method which I retrieved from LOG file.
这些是来自我从LOG文件中检索的相同方法的一些错误。
Any idea what could be causing this? OR I need to give some more detail?
什么可能导致这个?或者我需要提供更多细节?
1 个解决方案
#1
7
The following exception is encountered when a timeout is reached :
达到超时时遇到以下异常:
Error Message:The underlying connection was closed: An unexpected error occurred on a receive.
错误消息:基础连接已关闭:接收时发生意外错误。
As explained in the msdn documentation the default value of the timeout is infinite but the msdn documentation contains a mistake: http://msdn.microsoft.com/fr-fr/library/vstudio/system.net.ftpwebrequest.timeout(v=vs.80).aspx
正如msdn文档中所解释的那样,超时的默认值是无限的,但msdn文档包含一个错误:http://msdn.microsoft.com/fr-fr/library/vstudio/system.net.ftpwebrequest.timeout(v= VS.80)的.aspx
In fact, the default value is 100000 ms (1 min and 40 sec) so you can declare the value Timeout to infinite with : oFtpWebRequest.Timeout = -1;
实际上,默认值为100000毫秒(1分40秒),因此您可以使用以下函数将值Timeout声明为无限:oFtpWebRequest.Timeout = -1;
#1
7
The following exception is encountered when a timeout is reached :
达到超时时遇到以下异常:
Error Message:The underlying connection was closed: An unexpected error occurred on a receive.
错误消息:基础连接已关闭:接收时发生意外错误。
As explained in the msdn documentation the default value of the timeout is infinite but the msdn documentation contains a mistake: http://msdn.microsoft.com/fr-fr/library/vstudio/system.net.ftpwebrequest.timeout(v=vs.80).aspx
正如msdn文档中所解释的那样,超时的默认值是无限的,但msdn文档包含一个错误:http://msdn.microsoft.com/fr-fr/library/vstudio/system.net.ftpwebrequest.timeout(v= VS.80)的.aspx
In fact, the default value is 100000 ms (1 min and 40 sec) so you can declare the value Timeout to infinite with : oFtpWebRequest.Timeout = -1;
实际上,默认值为100000毫秒(1分40秒),因此您可以使用以下函数将值Timeout声明为无限:oFtpWebRequest.Timeout = -1;