Here you go. You'll notice that there is no mention of base64 as this is coming from a file stream. If you are creating an attachment from a text string, you will need to encode as base 64. There is a base64 lib in Axis, but I don't know how good it is.
private void DoAttachment() throws IOException { File f = new File("test.doc"); InputStream is = new FileInputStream(f); byte[] inbuff = new byte[(int)f.length()];
is.read(inbuff); Attachment attach = new Attachment(); attach.setBody(inbuff); //Do not attempt to set body length, this calculated //be salesforce.com //attach.setBodyLength(new Integer(inbuff.length)); attach.setName("text.doc"); ID parentId = new ID(); parentId.setValue("00330000006ZpSZ"); attach.setParentId(parentId); SoapBindingStub b = loginEnterprise("mobile2@user.com", "pass"); SaveResult sr = b.create(new com.sforce.soap.enterprise.sobject.SObject[] {attach})[0]; if (sr.isSuccess()) System.out.println("Successfully added attachment."); else System.out.println("Error adding attachment: " + sr.getErrors(0).getMessage()); }
Here you go. You'll notice that there is no mention of base64 as this is coming from a file stream. If you are creating an attachment from a text string, you will need to encode as base 64. There is a base64 lib in Axis, but I don't know how good it is.
private void DoAttachment() throws IOException {
File f = new File("test.doc");
InputStream is = new FileInputStream(f);
byte[] inbuff = new byte[(int)f.length()];
is.read(inbuff);
Attachment attach = new Attachment();
attach.setBody(inbuff);
//Do not attempt to set body length, this calculated
//be salesforce.com
//attach.setBodyLength(new Integer(inbuff.length));
attach.setName("text.doc");
ID parentId = new ID();
parentId.setValue("00330000006ZpSZ");
attach.setParentId(parentId);
SoapBindingStub b = loginEnterprise("mobile2@user.com", "pass");
SaveResult sr = b.create(new com.sforce.soap.enterprise.sobject.SObject[] {attach})[0];
if (sr.isSuccess())
System.out.println("Successfully added attachment.");
else
System.out.println("Error adding attachment: " + sr.getErrors(0).getMessage());
}
Message Edited by adamg on 02-17-2005 11:36 AM