You need to sign in to do that
Don't have an account?
TheCustomCloud
inputFile keeps adding the file again when refreshing the page
I select a file, I click the button to save the file as an attachment. This all works fine. But if I then refresh the browser it adds that same file a second time. If I refresh again it again adds a 3rd file and so on..
Anyone know how to fix this? I tried nulling the attachment out in the controller but it doesnt help :(
Here is the inputfile and command button in the VF page.
Here is the save method in the controller
Here is the end result with multiple copies of the same file being attached after I clicked refresh twice.
Anyone know how to fix this? I tried nulling the attachment out in the controller but it doesnt help :(
Here is the inputfile and command button in the VF page.
<apex:pageBlockButtons location="top"> <apex:inputFile styleClass="btn" value="{!anAttachment.Body}" fileName="{!anAttachment.Name}"/> <apex:commandButton id="saveButton" value="{!$Label.Save_Attachment}" action="{!saveAttachment}"/> </apex:pageBlockButtons>
Here is the save method in the controller
// save attachment to service case public void saveAttachment(){ // create bridge object for additional functionality Attachment__c bridge = new Attachment__c(); bridge.External__c = false; bridge.Restricted__c = false; bridge.Service_Case__c = theCase.York_Case__c; insert bridge; // create attachment and add it to the bridge object anAttachment.parentId = bridge.id; insert anAttachment; anAttachment = null; anAttachment = new Attachment(); }
Here is the end result with multiple copies of the same file being attached after I clicked refresh twice.
return ApexPages.currentPage().setRedirect(true);
All Answers
public pageReference saveAttachment(){
// your existing code
// return a reference to your VF page with appropriate parameters
return new pageReference('/apex/VFPageName' );
}
This must be a problem other developers are encountering with the inputFile apex tag.
Also, you can try adding the following IF statement in your saveAttachment method.
if (anAttachment.Body != null) {
// your existing code
}
I tried checking for null but no change, it still creates duplicates on refresh. :(
return ApexPages.currentPage().setRedirect(true);