3

/Controller.cls/

public class docxVFcontroller {
public string base64text {get; set;}
public void getFile()
        {
         Attachment a = [Select Body from attachment where name like '%word.docx%' limit 1];
         blob b = a.body;

         base64text  = EncodingUtil.base64Encode(b);
        }
}

/VF page/ ...

<script type="text/javascript">
var base64 = '{!base64text}';

/* assuming on click of command button i have this base64 text.Please suggest an idea on how would i process this base64 data to json or som other format to process further. Or is there any way to process it in controller only to a json String */

</script>
5
  • I think basically you're trying to do a base64decode on a string in JS. Maybe this can help you: stackoverflow.com/questions/246801/… Commented Aug 19, 2013 at 9:31
  • Not sure what you're trying to achieve after converting it to String...Inorder to convert it to json use JSON.serialize() method Commented Aug 19, 2013 at 9:31
  • I have a question. The base64 data i receive is of word document. Does it matter when decoding?
    – Soberano
    Commented Aug 19, 2013 at 9:43
  • @ForceDeveloper : I want to read the word document and then parse the content after which i will be creating/deleting records. So I want to parse the data basically. But the data i have is base64. So finding way to get the actual data. And thank you for your response.
    – Soberano
    Commented Aug 19, 2013 at 9:46
  • why not use the EncodingUtil.base64decode(yourBase64Blob) so you can manipulate it as a string? Then you can use all the string methods... Commented Aug 19, 2013 at 14:59

2 Answers 2

5

A docx file is actually a zip file (or more precisely an Open Office file), which is a binary file containing text files. There is no Apex native support for unzipping files to get at the contents within e.g. document.xml. However in this two part blog series you can find two alternative approaches to unzipping and accessing the docx contents, both still native to the platform (no third party services). Source code is linked in the blog. Once you have the document.xml file, you can use the built in Apex XML parser.

Option 1 : Unzip via Static Resoruce

NOTE: If you have the .docx file as an attachment already you can skip the first screen.

enter image description here

enter image description here

Option 2: Unzip via jszip library

NOTE: This shows a .xlsx file, though the process is the same for docx

enter image description here

0

I see you are already using the EncodingUtil Class from Salesforce. Why not use this class to decode your base64 data into a string so you can use any and all string methods to parse it how you like?

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm

Name            Arguments             Return Type       Description
-------------------------------------------------------------------------------------
base64Decode    String inputString    Blob              Converts a Base64-encoded String to a Blob representing its normal form.
base64Encode    Blob inputBlob        String            Converts a Blob to an unencoded String representing its normal form.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .