[Q] FTP upload question

Hannes Hirzel hirzel at spw.unizh.ch
Thu Feb 7 12:51:39 UTC 2002


On Thu, 7 Feb 2002, Bergel Alexandre wrote:

> Hello,
> 
> On Tue, Feb 05, 2002 at 03:23:10PM +0100, Hannes Hirzel wrote:
> > Hi 
> > 
> > how do I save a string on an ftp server as a file?
> > 
> > - open the connection (giving user name and password)
> > 
> > - put the string as a file on the server
> > 
> > - close the connection
> 
> You can use ServerDirectory.
> 
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> s:=ServerDirectory new.
> s type: #ftp.
> s server: 'YourServer.com'.
> s user: 'username'.
> s password: 'yourpass'.
> s openFTP.
> 
> stream:=s fileNamed: 'yourfile.txt'.
> stream nextPutAll: 'yourString'.
> stream close.
> 
> s quit.
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> 
Thank you, Alexandre. It works!!!

I had to give the directory path as well 

- and actually the full path I get when I issue the 'pwd' command when
doing ftp from the OS command line -

to make it work.


Before I did this I got a walkback. The instance variable directory was
nil.

The walkback was in the method:

openNoDataFTP
	"Open a connection to the directory and server I hold.  Return a
FTPSocket.  No dataPort is opened.  When you are all done, be sure to tell
the socket to QUIT, and then destroy it."

	| so rr serverIP what |
	Socket initializeNetwork.
	socket ifNotNil: [socket isValid 
			ifTrue: [^ socket]	"already open"
			ifFalse: [socket _ nil. ]].
"HH" Transcript show: 'socket _ nil'; cr.
	Cursor wait showWhile: [
		FTPSocket retry: [serverIP _ NetNameResolver
addressForName: server timeout: 20.
					serverIP ~~ nil] 
			asking: 'Trouble resolving server name "' , server
, '".  Keep trying?'
			ifGiveUp: [^ 'Could not resolve the server
named: ', server].
		so _ FTPSocket new.
		so portNum: 21.
		so connectTo: serverIP port: 21.  "21 is for the control
connection"
		so waitForConnectionUntil: FTPSocket standardDeadline.
		].

	Transcript cr; show: 'ftp: ', server; cr.
	(rr _ so lookFor: '220 ') == true ifFalse: [^ rr].	"220 para1
Microsoft FTP Service"
	[	"repeat both USER and PASS since some servers require it"
		so sendCommand: 'USER ', user.
		(rr _ so lookFor: '331 ') == true ifFalse: [^ rr].	"331
Password required"
		so sendCommand: 'PASS ', self password.		"will ask user,
if needed"
		 (rr _ so lookSoftlyFor: '230 ') == true
	] 	"230 User logged in"
		whileFalse: [
			rr first == $5 ifFalse: [^ rr].	"timeout"
			passwordHolder _ nil.
			what _ (PopUpMenu labels: 'enter password\give up'
withCRs) 
				startUpWithCaption: 'Would you like to try
another password?'.
			what = 1 ifFalse: [so destroy.  ^ rr]].
	


"*** walback here because directory beeing empty does not understand
isEmpty ***"
    directory isEmpty ifFalse: [
		so sendCommand: 'CWD ', directory.
		(rr _ so lookFor: '250 ') == true ifFalse: [^ rr].	"250
CWD successful"
	].
	"Need to ask for name of directory to make sure?"
	"socket _ so".	"If user wants to keep connnection open, he must
store socket"
	^ so




As I did not understand the code of the method I just tried

s := ServerDirectory new.
s type: #ftp.
s server: 'myServerId'.
s user: 'myUserName'.
s password: 'myPassWord'.
s directory: '/theFullPathOfMyHomeDirectory'.   "<-- added "
s openFTP.


stream:=s fileNamed: 'myfile.txt'.
stream nextPutAll: 'myString'.
stream close.

s quit.


And it worked. Great!
Thanks again. This is really helpful for my application to be able
to put files back on the web server.


Regards
Hannes Hirzel




More information about the Squeak-dev mailing list