Upload a Photo to FTP

Hell everybody, I have a Notes aplication (WEB). .I want to upload a photo to a FTP directory throw a lotusscript.

I have a form that have to upload a foto to a FTP with a “File Upload Control” is posible?

Thank You very much!

Subject: Upload a Photo to FTP

well, you can run an agent that makes the FTP job for you.

//Tommy

Subject: RE: Upload a Photo to FTP

Yes but I dont know how can I do it, have you any example? than you.

Subject: RE: Upload a Photo to FTP

I don’t have an example for this case but you can try this code

This agent uses a profiledoc to transfer the files to different FTP servers depending on the SendTo field. Used for incoming mails.

God luck!

The agent:

import lotus.domino.*;

import org.apache.commons.net.ftp.*;

import java.util.Vector;

import java.util.Enumeration;

import java.util.Hashtable;

import java.io.File;

import java.io.FileInputStream;

import java.io.BufferedInputStream;

import java.io.IOException;

public class FTPBrokerAgent extends ContextAgent {

private static final String DEF_SERVER = "ftpaddress";

private static final String DEF_USER = "ftpusername";

private static final String DEF_PWD = "ftppassword";

private static final String DEF_DIR = "ftpdirectory";



private FTPConf defConf;

private Hashtable lupConf = new Hashtable();

//private String tmpDir = System.getProperty( “user.dir” );

private View lupView = null;

public void ContextMain() throws Exception {

defConf = new FTPConf( DEF_SERVER, DEF_USER, DEF_PWD, DEF_DIR );

View view = curDb.getView( “new4ftp” );

lupView = curDb.getView( “lupEmail” );

view.refresh();

if ( lupView != null ) lupView.refresh();

session.setConvertMIME( false );

Document doc = view.getFirstDocument();

while ( doc != null ) {

Document nextDoc = view.getNextDocument( doc );

FTPConf conf = lookupConf( doc.getItemValueString( “SendTo” ));

FTPClient ftp = conf.getFTPClient();

System.out.println( “—===—===—===—” );

System.out.println( "sendTo: " + doc.getItemValueString( “SendTo” ) );

System.out.println( "server: " + conf.getServer() );

System.out.println( "user: " + conf.getUser() );

System.out.println( “pwd: ******” );

System.out.println( "dir: " + conf.getDir() );

System.out.println( “—===—===—===—” );

Item item = doc.getFirstItem( “Body” );

if ( item.getType() == Item.RICHTEXT ) {

System.out.println( "1" );

RichTextItem rti = (RichTextItem)item;



Vector v = rti.getEmbeddedObjects();

Enumeration e = v.elements();

while ( e.hasMoreElements() ) {

 System.out.println( "2" );

 EmbeddedObject eo = (EmbeddedObject)e.nextElement();

 

 if ( eo.getType() == EmbeddedObject.EMBED_ATTACHMENT ) {

  System.out.println( "3" );

  

  String fName = eo.getName();

  boolean success = ftp.storeFile( fName, eo.getInputStream() );

  

  System.out.println( "SEND STATUS: " + success );

  

  if ( success ) {

   doc.replaceItemValue( "ftp", "ok" );

   doc.replaceItemValue( "ftptime", session.evaluate( "@Now" ));

  } else {

   doc.replaceItemValue( "ftp", ftp.getReplyString());

  }

  doc.save( true, true );

  

 }

}

} else if ( item.getType() == Item.MIME_PART ) {

System.out.println( "4" );

doc.computeWithForm( false, false );

Vector v = doc.getItemValue( "attachments" );



System.out.println( v.size() );



Enumeration e = v.elements();

while ( e.hasMoreElements() ) {

 System.out.println( "5" );

 String fName = (String)e.nextElement();

 

 EmbeddedObject eo = doc.getAttachment( fName );

 boolean success = ftp.storeFile( fName, eo.getInputStream() );

 

 System.out.println( "SEND STATUS: " + success );

  

 if ( success ) {

  doc.replaceItemValue( "ftp", "ok" );

  doc.replaceItemValue( "ftptime", session.evaluate( "@Now" ));

 } else {

  doc.replaceItemValue( "ftp", ftp.getReplyString());

 }

 doc.save( true, true );

}

}

doc.recycle();

doc = nextDoc;

}

for ( Enumeration e = lupConf.elements(); e.hasMoreElements(); ) {

FTPConf conf = (FTPConf)e.nextElement();

conf.close();

}

}

private FTPConf lookupConf( String sendTo ) {

FTPConf conf = defConf;

sendTo = sendTo.toLowerCase();

if ( lupConf.containsKey( sendTo )) {

conf = (FTPConf)lupConf.get( sendTo );

} else {

try {

if ( lupView == null ) return conf;



Document ldoc = lupView.getDocumentByKey( sendTo );



if ( ldoc != null ) {

 

 String srv = ldoc.getItemValueString( "Server" );

 String usr = ldoc.getItemValueString( "User" );

 String pwd = ldoc.getItemValueString( "Password" );

 String dir = ldoc.getItemValueString( "Directory" );

 

 if ( srv == null ) srv = DEF_SERVER;

 if ( usr == null ) usr = DEF_USER;

 if ( pwd == null ) pwd = DEF_PWD;

 if ( dir == null ) dir = DEF_DIR;

 

 conf = new FTPConf( srv, usr, pwd, dir );

 lupConf.put( sendTo, conf);

}

} catch ( NotesException ne ) {

// do nothing

} catch ( Exception e ) {

// do nothing

}

}

return conf;

}

}

Script Lib:

import org.apache.commons.net.ftp.*;

import java.io.IOException;

public class FTPConf {

private String srv = null;

private String usr = null;

private String pwd = null;

private String dir = null;



private FTPClient ftp = new FTPClient();



public FTPConf( String srv, String usr, String pwd, String dir ) throws Exception {

	this.srv = srv;

	this.usr = usr;

	this.pwd = pwd;

	this.dir = dir;

	

	connect();

}



private void connect() throws Exception {

	ftp.connect( srv );

	ftp.login( usr, pwd );

	System.out.println( "Connected to " + srv + "." );

	System.out.print( ftp.getReplyString() );

	

	int reply = ftp.getReplyCode();

	if( !FTPReply.isPositiveCompletion( reply )) {

		throw new IOException( "FTP server refused connection." );

	}

	

	ftp.enterLocalPassiveMode();

	ftp.setFileType( FTP.BINARY_FILE_TYPE );

	

	ftp.changeWorkingDirectory( "/" + dir );

}



public String getServer() {

	return srv;

}



public String getUser() {

	return usr;

}



public String getPwd() {

	return pwd;

}



public String getDir() {

	return dir;

}



public FTPClient getFTPClient() throws Exception {

	if( !ftp.isConnected() ) {

		connect();

	}

	

	return ftp;

}



public void close() {

	if( ftp.isConnected() ) {

		try {

			ftp.disconnect();

		} catch( IOException f ) {

			// do nothing

		}

	}

}

}

Subject: RE: Upload a Photo to FTP

This one is platform independent.

Pedro, you can trigger the agent on webquerysave or with a button.

//Tommy

Subject: RE: Upload a Photo to FTP

Pedro

Search the sandbox for the NotesFTP script library. As long as your server is running on windows you can use this to upload and download files via ftp.

Ben

Subject: RE: Upload a Photo to FTP

I Have read de SandBox, but in there, there are only for client applications, not for Web aplications.i have tried File Upload System from the SandBox but I think that doesn’t work in Web. :frowning:

Subject: RE: Upload a Photo to FTP

If you want to place files directly on to an ftp server, then you will need to write a java applet or activex control.

However, you can use the NotesFTP library in a WebQuerySave agent to ftp the file when the user saves the form over the web. Just use a standard file upload control.

Ben