作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这个函数中遇到了很多不同的问题:
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;
}
最佳答案
达到超时时会遇到以下异常:
Error Message:The underlying connection was closed: An unexpected error occurred on a receive.
关于asp.net - 将图像上传到 FTP 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251198/
我是一名优秀的程序员,十分优秀!