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
Kenji778Kenji778 

Create Attachment from posted data

Hey all,

I am working on a multiple file uploader component and have hit a bit of a wall. My component will post each file to a visualfroce page. That visualforce page must in turn read the data that was posted (serialized as binary/base64) and create an attachment out of that. I just get a bit confused about the interplay between binary/base64/blob data. So if you know the answers to any of these questions, please let me know.

 

1) When data is in a regular "file" type form field, and that form is submitted, how is that file data encoded?
 

2) When creating an attachment object would the body be base64/binary/blob?

 

Currently I have a simple class that takes the data passed in the Files field and attempts to convert the content and attach it to the log object i makes.. It runs, but the attachment just displays the raw content of whatever it was passed (in this case I passed in the string (linebreaks removed of course)

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1b

GFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQg

YnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhY

mxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55I

GNhcm5hbCBwbGVhc3VyZS4=

 

This is my method

 

    public void attachToObject()
    {
        
        Map<string,string> params = ApexPages.currentPage().getParameters();
        string fileData = params.get('Files');
        blob fileContent = blob.valueOf(fileData);      
        Data_Log__c log = new Data_Log__c();          
        log.trace__c = fileData;
        insert log;
        
        Attachment a = new Attachment(parentId = log.id, name='Data', body = fileContent);
        insert a;
        
        jsonString = '[{"delete_url":"NOT AVAILABLE","thumbnail_url":"The File","name":"'+log.id+'","delete_type":"DELETE","url":"theFile","size":'+fileContent.size()+'}]';
        
    }

 

Any help is appreciate. I feel like I have a loose understanding of what I need to do, but it's still a little foggy. If anyone could even just outline the steps I'd need to take to make this happen that would be awesome. 

 

TL;DR: How would I take data posted from a file form field and turn it into an attachment of the same type of file as the original?

Kenji778Kenji778

Also, the reason I am not using the built in file upload functionality is because I want this to support multiple file uploads and a much different look and feel is required. I am modifying an uploader built in jQuery that supports drag and drop, multiple files, queing, etc. It posts the files one by one to any page I specify, so I am just trying to build the handler for the files. The page talks a bit about setting it up. Notice especailly the page about setting it up without PHP.

https://github.com/blueimp/jQuery-File-Upload/wiki/Setup

 

Also this page has a section about setting it up for different server side languages. Sadly there isn't one for Apex :P

https://github.com/blueimp/jQuery-File-Upload/wiki

 

Basically pretend you were trying to build a normal file upload handler on your webserver, except I need to do in Apex. There is my question in a nutshell.

 

Shashikant SharmaShashikant Sharma

Try to give ContentType field appropriate value for the Attachment object record that you insert  . I hope this will solve your problem , please let me know if any issues in it.

Shashikant SharmaShashikant Sharma
Kenji778Kenji778

Would I just set it to base64 or what? The guide at http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_attachment.htm 
is a bit vauge. Do I set it as the content type of the actual file, or as the content being used to create the file? As far as I can tell the content coming in the post request would be base64 (certainly correct me if I am wrong) but the final file type could be anything (text documents, pictures, etc) 

Kenji778Kenji778

I did find that guide, but he is using the visualforce upload component, which I am not. Not sure how much that changes the game, but it seems to have some properties a normal upload component would not.

 

Also I was looking more into the content type attribute and found this:
http://boards.developerforce.com/t5/Java-Development/Allowed-attachment-content-type-values-in-API-calls/td-p/18257

it seems that i probably shouldn't have to mess with it, since it's set by the posting page, correct? It should already be in the headers. Or would it just say like 'multipart/form-encoded' in the headers and hence be all screwy? If that is the case then how would I know what the content type actually is, if it's just getting bassed a big base64 blob thingy? 

Kenji778Kenji778

A bit of an update. I have my uploader passing "data" now. if I capture the value passed in from the form field, it looks something like 

 

/home/sfdc/salesforce/sfdc/jsp/form/form1091191222.tmp

 

so how do I take that path to a file and turn it into an attachment?