You need to sign in to do that
Don't have an account?
Leonard
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.
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
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 :)
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 :)
Argh. I was looking at the AS3.0 doc on FileReference and that doesn't mention load().
Thanks. All clear now.
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
How to do file upload for any SObject using Visualforce.
rDr4g0n, thanks for that; the Base64 thing got me really stumped and was looking all over for a way to convert the ByteArray, thanks!