I'm uploading a file via File API inserting into Attachment Object, on a visualforce page in javascript.

When i query that body of the Attachment, the file is not readable. I tried decoding but my understanding is limited it seems. Any advice would be helpful.

here is the javascript code

function uploadFile() {
           var file = document.getElementById('fileToUpload').files[0];
            var reader = new FileReader();
            reader.onload = loaded;
            reader.onerror = errorHandler;


       function uploadAttachment(filecontent, filename, parentId, filedescription, filetype) {
            sforce.connection.sessionId = '{!$Api.Session_ID}';
            var attachment         = new sforce.SObject('Attachment');
            attachment.Name        = filename;
            attachment.IsPrivate   = document.getElementById('{!$Component.pg.frm.attachment}').checked;
            attachment.ContentType = filetype;
            attachment.Body        = filecontent;
            attachment.Description = filedescription;
            attachment.ParentId    = '{!$CurrentPage.id}';
            var result = sforce.connection.create([attachment]);

            var Id = result[0].id;

            if (result[0].getBoolean("success")) {

            } else {
              alert("failed to create attachment " + result[0]);

here is my controller.

public PageReference addAttachment() {

    List<Attachment> attchnts = [select id, name, Body, IsPrivate, ParentId, Description, ContentType from Attachment where id =:attchtid];       
    for(Attachment at : attchnts){
        filedescription = at.Description;
        //attachmentbody = Blob.valueOf(EncodingUtil.base64Encode(at.Body));
        attachmentbody = at.Body;
        filename = at.Name;


    if(filecontents == null) {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please choose a file type.');
    } else if(attachmentbody == null){
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file.');
  • why are you using javascript? you can use controller for file uploading purpose. Any specific reasons? Aug 8 '17 at 19:02
  • 15MB limit via visualforce page made me use javascript Aug 8 '17 at 19:13

Using FileReader#readAsDataURL means that some additional data would be prefixed to the filecontent parameter ("data:category/spec;base64,...."). You haven't shown us the "loaded" function, but you need to make sure that you've stripped this part off. You might need to debug the network connection to see what's being sent over the wire, but at minimum, make sure you're stripping off the leading content (something like fileBody.split(/,/)[1]).

  • Thank you! @sfdcfox that did the trick. splitting the body worked fine. Aug 8 '17 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.