Activity Forums Salesforce® Discussions How to attach file using Salesforce Lightning for Salesforce1?

  • Ravi

    Member
    November 4, 2016 at 1:43 pm

    Hi Tanu,

    You can do this using base64 encoding . You have to follow these steps.

    1.Use JavaScript's FileReader's readAsDataURL to read the file selected by the file input into a String variable.
    2.Encode the contents using JavaScript's encodeURIComponent function.
    3.Call the @AuraEnabled action with the contents, content type, and the Id of the parent record.
    4.URI decode the contents.
    5.Base64 decode the contents.
    6.Insert the attachment.

  • Parul

    Member
    September 21, 2018 at 1:18 pm

    Hi,

    For this Please use base64 encoding .

    1.Use JavaScript’s FileReader’s readAsDataURL to read the file selected by the file input into a String variable.
    2.Encode the contents using JavaScript’s encodeURIComponent function.
    3.Call the @AuraEnabled action with the contents, content type, and the Id of the parent record.
    4.URI decode the contents.
    5.Base64 decode the contents.
    6.Insert the attachment.

    Thanks

  • shariq

    Member
    September 22, 2018 at 3:02 am

    Hi,

    I found this online -

    This can be done with base64 encoding. The general pattern is as follows:

    Use JavaScript's FileReader's readAsDataURL to read the file selected by the file input into a String variable.
    Encode the contents using JavaScript's encodeURIComponent function.
    Call the @AuraEnabled action with the contents, content type, and the Id of the parent record.
    URI decode the contents.
    Base64 decode the contents
    Insert the attachment.
    There are limitations in the size of the file that can be transferred. I observed that limitation to be 1,000,000 characters. The solution to that is to chunk the file in to different pieces. That algorithm is slightly different, but basically boils down to iterating over the contents in chunks, and then concatenating them to the existing Attachment body.

    Use JavaScript's FileReader's readAsDataURL to read the file selected by the file input into a String variable.
    Call the @AuraEnabled action with the contents (URI encoded), content type, and the Id of the parent record.
    Insert the attachment (as done in the above steps) and return the Id back to the helper.
    For each chunk left:Call the @AuraEnabled action with the contents (URI encoded), and the Attachment Id.
    Retrieve the Attachment out of the DB (SOQL) and base64 encode its contents.
    Append the base64 chunk passed into the method with the existing body.
    Base64 decode it back into the body of the attachment.
    Update the attachment.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos