+ Start a Discussion
TheCustomCloudTheCustomCloud 

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.
<apex:pageBlockButtons location="top">
            <apex:inputFile styleClass="btn" value="{!anAttachment.Body}" fileName="{!anAttachment.Name}"/>&nbsp;
            <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.

  User-added image

Best Answer chosen by TheCustomCloud
TheCustomCloudTheCustomCloud
Turns out only way I can force it not to clear the attachment after saving is to do a client redirect.  This works for my scenario but I know it wont work for scenarios where you want to keep the state.

return ApexPages.currentPage().setRedirect(true);

All Answers

Kiran  KurellaKiran Kurella
You can try re-directing the page in the saveAttachment function as follows:

public pageReference saveAttachment(){
     // your existing code
 
    // return a reference to your VF page with appropriate parameters
    return new pageReference('/apex/VFPageName' );
}
TheCustomCloudTheCustomCloud
I tried returning the pagereference to itself but no luck, it still creates duplicate attachments on refresh :(

This must be a problem other developers are encountering with the inputFile apex tag.
Kiran  KurellaKiran Kurella
Do you have any JavaScript code on your page load or any other section, which is resubmitting the page?

Also, you can try adding the following IF statement in your saveAttachment method.

if (anAttachment.Body != null) {
    // your existing code
}
TheCustomCloudTheCustomCloud
No javascript running.  This is one of the simplest pages I've written, that is the confusing part.


I tried checking for null but no change, it still creates duplicates on refresh. :(
TheCustomCloudTheCustomCloud
Turns out only way I can force it not to clear the attachment after saving is to do a client redirect.  This works for my scenario but I know it wont work for scenarios where you want to keep the state.

return ApexPages.currentPage().setRedirect(true);
This was selected as the best answer
redfin.ax1152redfin.ax1152
This continues to be a problem if you want to keep the page's current state.