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;
   }
}