2

I'm trying to pass a variable from a javascript inside a VF page to a varibale in the Apex Controller.

The VF page code is:

<apex:page controller="uploadDataTosObject" apiVersion="36.0"> 
[Code....]                  
    <apex:form>
        <script>
            function setVal(result)
            {
                document.getElementById("{!$Component.hdnField}").value =result;
            }
        </script>
        <apex:inputHidden id="hdnField" value="{!myblob}" />
        <apex:commandButton value="Post Page" action="{!post}" />
    </apex:form>
    <script>
        setVal();
    </script>                                                                                            
    <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.js">
   </script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js">    </script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
     [Code...]    
            function to_csv(workbook) {
                var result = [];
                workbook.SheetNames.forEach(function(sheetName) {
                    var csv = X.utils.sheet_to_csv(workbook.Sheets[sheetName]);
                    if(csv.length > 0){
                        result.push("SHEET: " + sheetName);
                        result.push("");
                        result.push(csv);
                    }
                });
                output = setval(result.join("\n"));
                return result.join("\n");
            }                
[Code...]
    </body>
</apex:page>

this is the VF controller:

public with sharing class uploadDataTosObject {       
    public String myblob {get; set;}        
    public void post(){
        system.debug(myblob);
    }
}

I need to assaign "result.join("\n")" to the variable "myblob" inside the controller. Now I'm trying to use an "apex:inputhidden", but the System.debug shows "undefined" in the Logs. How can I assign the variable "result.join()" to "myblol"?

  • 1
    You'll have to assign it to a string. Something in your controller like public void setContent(String value) { myBlob = Blob.valueOf(value); }. Then bind your inputHidden to content. – Adrian Larson Sep 30 '16 at 13:28
1

I'm not able to test if this works currently, but here's what I expect you need:

Page

<apex:page controller="MyClass">
    <apex:inputHidden value="{!content}" ...>
</apex:page>

Controller

public MyClass
{
    public Blob myBlob { get; private set; }
    public String content
    {
        get;
        set { myBlob = Blob.valueOf(value); }
    }
}

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.