您好我试图上传一个传入的字节[]并获得550的文件。
我在using语句中使用了一个Memory流,所以我认为它应该很好地关闭自己。
如果我处理字节数组而不是物理文件,可能导致锁定,因为字节数组没有映射到文件,因为它是由csv帮助程序生成的。 / p>
基本上我用stringbuilder构建一个csv,然后调用Export,它接受字符串构建器并将其放入byte [],然后传入此方法..
只是想知道是否有人能看出我做错了什么。 谢谢!
bool PutFileToServer(string remoteFile, byte[] bytesToUpload, bool result)
{
try
{
LogStatus("Creating FTP Request");
var ftpRequest = CreateRequest(remoteFile, WebRequestMethods.Ftp.UploadFile);
using (var dataStream = new MemoryStream(bytesToUpload))
using (var requestStream = ftpRequest.GetRequestStream())
{
dataStream.Position = 0;
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = dataStream.Read(byteBuffer, 0, bufferSize);
LogStatus("Reading File Stream");
try
{
while (bytesSent != 0)
{
requestStream.Write(byteBuffer, 0, bytesSent);
bytesSent = dataStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
LogStatus("Error Reading File Bytes");
LogStatus(ex.Message);
result = false;
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
if (dataStream != null)
{
dataStream.Close();
}
if (requestStream != null)
{
requestStream.Close();
}
if (ftpStream != null)
{
ftpStream.Close();
}
ftpRequest = null;
}
result = true;
}
}
catch (Exception ex)
{
LogStatus(ex.Message);
result = false;
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
if (ftpStream != null)
{
ftpStream.Close();
}
ftpRequest = null;
}
return result;
}