function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LeonardLeonard 

how do i upload attachments to salesforce via flex?

I can't capture the binary data of a local file to pass to an Attachment SObject in flex. There doesn't seem to be anything in the swc to handle file uploads.
Best Answer chosen by Admin (Salesforce Developers) 
rDr4g0nrDr4g0n

Here is the function I used to ul attachments:

 

 

 

private function uploadAttachment(event:Event):void{ var theFile:Object = event.target; var attachment:SObject = new SObject(); var encoder:Base64Encoder = new Base64Encoder; encoder.encodeBytes(theFile.data); attachment.type = "Attachment"; attachment.Body = encoder.toString(); attachment.ParentId = this.id; attachment.Name = theFile.name; SF.create([attachment], new AsyncResponder(uploadAttachmentSuccess, handleFault), false); }

This function is called after a user selects a file to ul, and the load() method has been called and completed on that fileReference. The file has to be Base64 encoded before you upload it. Then I created a new SObject and supplied the necessary properties: type, Body, ParentId, and Name. The ParentId is the Id of the salesforce Object that the file will be associated with.

 

Then I called the "create" method on my salesforce connection, SF. The create method takes an array of objects to create, and an AsyncResponder to handle the success or failure of the upload.

 

This function doesn't check for the 5mb filesize limit, so you will need to do that!!!

 

Hope this helps :)

 

All Answers

rDr4g0nrDr4g0n

Here is the function I used to ul attachments:

 

 

 

private function uploadAttachment(event:Event):void{ var theFile:Object = event.target; var attachment:SObject = new SObject(); var encoder:Base64Encoder = new Base64Encoder; encoder.encodeBytes(theFile.data); attachment.type = "Attachment"; attachment.Body = encoder.toString(); attachment.ParentId = this.id; attachment.Name = theFile.name; SF.create([attachment], new AsyncResponder(uploadAttachmentSuccess, handleFault), false); }

This function is called after a user selects a file to ul, and the load() method has been called and completed on that fileReference. The file has to be Base64 encoded before you upload it. Then I created a new SObject and supplied the necessary properties: type, Body, ParentId, and Name. The ParentId is the Id of the salesforce Object that the file will be associated with.

 

Then I called the "create" method on my salesforce connection, SF. The create method takes an array of objects to create, and an AsyncResponder to handle the success or failure of the upload.

 

This function doesn't check for the 5mb filesize limit, so you will need to do that!!!

 

Hope this helps :)

 

This was selected as the best answer
LeonardLeonard
Wow, that's great. But I'm not clear on what you're doing to generate that event. What are you doing to generate the load() event? Do you mean upload? FileReference doesn't have a load method.
rDr4g0nrDr4g0n

FileReference has a load() method as described here: http://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.html#load%28%29

 

Here are the other functions for the selecting of the files, etc:

 

 

private function selectAttachment():void{
fR = new FileReference();
fR.addEventListener(Event.SELECT, uploadAttachmentSelected);
fR.addEventListener(Event.COMPLETE, uploadAttachment);
fR.browse();
}

private function uploadAttachmentSelected(event:Event):void{
event.target.load();
}

 Once a file is selected, the uploadAttachmentSelected function is called which loads the selected file into the file reference. Once that loading is done, the COMPLETE event is fired which calls the uploadAttachments function I posted in my first post.

 

Hopefully that clarifies things for you :)

 

Message Edited by rDr4g0n on 02-02-2010 09:06 AM
LeonardLeonard

Argh. I was looking at the AS3.0 doc on FileReference and that doesn't mention load().

 

Thanks. All clear now.

LeonardLeonard
So it turns out FileReference.load() is only available through Flex 3.5. Annoying.
rDr4g0nrDr4g0n
Is there a particular reason you need to use an older version than 3.5? It's pretty easy to update and you can revert back easily enough as well.
dmedme

Hey guys,

 

Did any of you worked on how to delete an attachments from salesforce via flex?  If so pls share your snippets.

 

thanks

dme

ministe2003ministe2003

rDr4g0n, thanks for that; the Base64 thing got me really stumped and was looking all over for a way to convert the ByteArray, thanks!