2

I dont want to refresh the whole page after completing upload.

This is my VF Page,it directly close the modal box and refresh the whole page after I click Upload button.I dont want it to refresh or close the modal box until I click CLose.However it successfully upload file,it just behavior that I would like to change.

<div class="modal" id="attModal" tabindex="-1" role="dialog" aria-labelledby="attModal" aria-hidden="true">
        <apex:outputPanel id="uploadForm">
        <apex:form >
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" >&times;</button>
                <h4 class="modal-title" id="myModalQuesLabel">File Attachment</h4>
            </div>
            <p class="errorMessage" style="color:red"><apex:outputText value="{!attErrorMsg}"/></p>
            <div class="modal-body">
                <input type="hidden" name="resId" id="resId" value=""  class="form-control"/>
                <div class="form-group">
                  <label  class="control-label">1. Select the File</label> 
                    <apex:inputFile value="{!newAtt.body}" filename="{!newAtt.name}"/>
                </div>

                <div class="form-group">
                  <label  class="control-label">2. Attach File</label>&nbsp;           
                  <apex:commandLink styleclass="btn-bootstrap btn-primary btn-sm" action="{!uploadFile}" immediate="false" status="loadingStatus" >
                    Upload
                  </apex:commandLink>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn-bootstrap btn-default btn-sm" data-dismiss="modal" data-loading-text="Loading...">Close</button> 
            </div>
          </div>
        </div>
        </apex:form>
        </apex:outputPanel>
      </div>

This is my controller

public PageReference uploadFile(){
        newAtt.ParentId = header.Id;


        if( newAtt.Name != null ){
            Integer s = 0;
            newAtt.ParentId = header.Id;
            Blob temp = newAtt.Body;
            s = temp.size();
            Double size = (s / (1024*1024));

            if(size > 5){
                attErrorMsg = 'You cannot upload file more than 5MB size';
                newAtt =  new Attachment();  // done to prevent passing back to viewstate
                return null;
            }
            else{
                try{
                    insert newAtt;
                }
                catch(Exception ex){
                    attErrorMsg= 'File attach error:'+ ex.getMessage();
                    return null;
                }
            }

            newAtt = new Attachment();
            closeAttPopup = 'true';

          return null;

        }
        else{
            attErrorMsg = 'Please SELECT a file to upload.';
            newAtt =  new Attachment();  
            return null;
        }
    }   
3

With the current approach, having the commandLink, the form will be automatically submitted to the server and you can't prevent that considering that you want to know whether the upload was successful or not.

A better way of doing that will be to switch to Visualforce Remoting, push the file to the server and manipulate your DOM based on the response. This is a great post that explains how all that ties together - Uploading Attachments to Salesforce using Javascript Remoting

  • i will try this one and will update the result here. – unidha Jul 1 '15 at 1:35
  • Yes, I tried this and it works. – unidha Jul 2 '15 at 7:29
1

Use rerender to refresh parts of the page you want.

<apex:outputPanel id="areaToRefresh">
     <!--refreshed part -->
<apex:outputPanel>
<apex:commandLink rerender="areaToRefresh">

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.