Wednesday 4 November 2015

apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute.

This is the basic problem in apex .So we can use html input file and java script to solve this type of issue .

Below code will help you avoid the issue .

Visual force Page
<apex:page id="pgid" controller="InputFileUsingHtmlJavaScript">
<apex:form id="frm">
<input type="file" id="fileId" name="attFile" /><br/>
<apex:actionFunction name="passToController" action="{!doAttachment}" reRender="frm">
<apex:param name="fname" value="" assignTo="{!filename}" />
<apex:param name="body" value="" assignTo="{!body}"/>
</apex:actionFunction>
<apex:commandButton value="Save" onclick="remoteLocationPost();" />
<script>
var blobfile;
function getAsText(readFile) {
var reader = new FileReader();
reader.readAsDataURL(readFile);
reader.onload = attLoaded;
}
function attLoaded(evt) {  
var fileString = evt.target.result;
blobfile = fileString;
var  input = document.getElementById("fileId");
var filename= input.value;          
passToController(filename,blobfile);
}
function remoteLocationPost(){
var fbody= document.getElementById("fileId").files[0];
getAsText(fbody); 
}   
</script>
</apex:form>
</apex:page>
Apex Class 
  public class InputFileUsingHtmlJavaScript{
    public String filename{get;set;}
    public String body{get;set;}
    public void doAttachment(){ 
        Attachment at=new Attachment();
at.Name=filename;
at.Body=Blob.valueOf(body);
at.Parentid=//Needs to add your record id below which you need attachment ;
Insert at;
   }
}



16 comments:

  1. Hi Manoj,

    To configure a Custom "View All" Button in Accounts View Related list for the Activities History.

    On Click of the Button, The View should contain a List of Activities(Tasks) and a Long Text Area field from a Custom Object for each activity.

    Acvitity Record Id is stored in the Custom Object Field.

    Can I use a Wrapper Class and add a list/field from the Custom Object instead of the Flag? Has anyone worked on a similar requirement?

    Thanks
    Amit

    ReplyDelete
  2. This solution worked for me. Thanks!!!

    ReplyDelete
    Replies
    1. this is not working for me ,could you help me on this please,
      i pasted same code and assigned record id but its not inserting any file to the record

      Delete
  3. This solution worked for me too..Cheers!! :)

    ReplyDelete
  4. I keep receiving a "Invalid Page Redirection"

    ReplyDelete
  5. I keep receiving a "Invalid Page Redirection"

    ReplyDelete
  6. I keep receiving a "Invalid Page Redirection

    ReplyDelete
  7. Thank you very much for your solution.. but when I try to download the attachment (EXCEL file), it is giving file is corrupted error message.. really appreciate if you can share some idea on that

    Thanks

    ReplyDelete
  8. I keep receiving a "Invalid Page Redirection"

    ReplyDelete
  9. Receving invalid page redirection.. any solution

    ReplyDelete
  10. body is blank when I uploading attachment

    ReplyDelete
  11. Hello Sir,

    I tried to run the above give code but it is passing blank body for attachment and also getting invalid page redirection error.
    Please help me out in this issue.

    ReplyDelete
  12. Hello,
    Getting null value in controller filename and body.

    ReplyDelete
  13. What is max size of the file this allows?

    ReplyDelete
  14. Hi

    its not inserting any file, and its not getting any errors

    ReplyDelete
  15. Could you plz help me on this , why its not inserting file to attachment , i assined parentId to record id

    ReplyDelete