Stream problem - WeOnlyDo Discussion board

Stream problem (General questions)

by TheChad2k6, Monday, May 15, 2006, 22:56 (6766 days ago)

I'm currently evaluating the FtpDLX.Net component. I need to be able to retrieve a (text) file from a SFTP server and save it to a database. This is for a middle tier component and I do not want to save it to a file.

I am able to send a file from a stream using PutFile, but when I try to retrieve a file, the stream is closed after the GetFile call and I get a System.ObjectDisposedException: Cannot access a closed Stream exception.

Here's the code, with all my exception handling, etc. removed...

[code]

public class FtpHelper {

private FtpDLX _ftpConn;

public FtpHelper(string connectToServer, string ftpLogin, string ftpPassword, bool isSFTP, bool usePassive) {

_ftpConn = new FtpDLX();

_ftpConn.Blocking = true;

_ftpConn.Hostname = connectToServer;
_ftpConn.Login = ftpLogin;
_ftpConn.Password = ftpPassword;
_ftpConn.Passive = usePassive;

_ftpConn.Protocol = isSFTP ? Protocols.SFTP : Protocols.FTP;

_ftpConn.Connect();

}

/// <summary>
/// Sends a string to the server as a file.
/// </summary>
public bool SendTextAsFile(string text, string fileName, string remotePath) {

try {

_ftpConn.TransferMode = TransferModes.AscII;

MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(text));

_ftpConn.LocalPath = fileName;
_ftpConn.PutFile(stream, remotePath);

stream.Close();
}

catch {

return false;

}

return true;


}

// Want this function to pickup the remote CSV file and
// return it as a string
public string PickupFileToString(string folder, string fileName) {

MemoryStream stream = new MemoryStream();

_ftpConn.TransferMode = TransferModes.AscII;

// I have to have this here or I get a
// WeOnlyDo.InvalidSettingException, even though I'm not saving
// the file?
_ftpConn.LocalPath = fileName;

// filename example: /out/batch00002.csv
_ftpConn.GetFile(stream, folder + / + fileName);

// Stream is not readable here... getting ObjectDisposedException
// I have tried using other GetFile calls
//(ie GetFile(string localPath, string
// remotePath)) and they work fine, so I know the filename is
// correct and the file is on the server

return StreamToText(stream);

}

private static string StreamToText(Stream stream) {

string tempString = null;
int count = 0;

StringBuilder sb = new StringBuilder();

byte[] buf = new byte[8192];

do {

count = stream.Read(buf, 0, buf.Length);

if (count != 0) {
tempString = Encoding.ASCII.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0);
return sb.ToString();

}


public void Disconnect() {
_ftpConn.Disconnect();
}

~FtpHelper() {

if (_ftpConn.State != States.Disconnected)
_ftpConn.Disconnect();

}

}

[/code]

Re: Stream problem

by TheChad2k6, Monday, May 15, 2006, 22:58 (6766 days ago) @ TheChad2k6

To clarify - it's in the PickupFileToString method of my class where I'm having the problem...

Thanks!

Re: Stream problem

by wodDrazen, Tuesday, May 16, 2006, 09:33 (6766 days ago) @ TheChad2k6

Hi,

What kind of database are you trying to use with wodFtpDLX.NET component for downloading and uploading files?

Regards,
Drazen

Re: Stream problem

by TheChad2k6, Tuesday, May 16, 2006, 15:10 (6765 days ago) @ wodDrazen

Hi,

What kind of database are you trying to use with wodFtpDLX.NET component for downloading and uploading files?

Regards,
Drazen

The back end is SQL Server, but I think it's unrelated to the problem. The helper function above needs to take a string, convert it to a stream, and upload it as a file to the server, and then it also needs to be able to download a file into a stream, and convert it to a string.

The upload part works great, but the download is where the issue is.

Basically, the problem I'm having is that the GetFile(stream, remotePath) function is not leaving the stream open, so my only option is to save the file to the local drive, import it and save it to the database, and then delete it. The file contains batches of credit card payments going to a 3rd party for authorization and it's a security risk to save it to the drive.

There is a similar problem posted here with your wodSFTP.Net component:

http://www.weonlydo.com/index.asp?forum=1&action=view&topic=1123526776#1123526776

When I try to reproduce that using FtpDLX (as follows), it does not work:

[code]

static void Main(string[] args) {

WeOnlyDo.Client.FtpDLX s = new FtpDLX();

s.Protocol = Protocols.SFTP;
s.Blocking = true;
s.Hostname = xxx.xxx.com ;
s.Login = login ;
s.Password = password ;
s.Connect();

Stream NewMsgStream = new MemoryStream();
//Stream is readable

s.LocalPath = c:000004.csv ;
s.GetFile(NewMsgStream, /out/0000004.csv );

//Stream is closed here, next line throws exception!!

NewMsgStream.Position = 0;
byte[] b = new Byte[20];
NewMsgStream.Read(b, 0, 20);
s.Disconnect();

}
[/code]

EDIT: This problem is only happening on a SFTP server. If I use the EXACT same code above (except changing the protocol to Protocols.FTP ), and connect to a standard FTP server instead, it works fine.

Re: Stream problem

by wodSupport, Tuesday, May 16, 2006, 16:55 (6765 days ago) @ TheChad2k6

Chad,

I believe this is now fixed. Please download wodFtpDLX.NET again (or request update if you're licensed user) and try it out. Make sure Version property returns 1.0.9.18.

Let me know how it goes!

Kreso