0

Controller

public with sharing class FileUploadController { 
    public Id accountId;
    public FileUploadController(ApexPages.StandardController controller) {
    accountId = controller.getId();

    }


    public Attachment document { 
    get { 
        if (document == null) 
        document = new Attachment(); 
    return document; 
    } 
   set; 
} 
public PageReference upload() {

   document.OwnerId = UserInfo.getUserId();
   document.ParentId = ApexPages.currentPage().getParameters().get('id'); // the record the file is attached to
   document.IsPrivate = true;
//document.AuthorId = UserInfo.getUserId(); 
//document.FolderId = UserInfo.getUserId(); // put it in running user's folder 
try { 
insert document;
} catch (DMLException e) { 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file')); 
return null; 
} finally { 
document.body = null; 
document = new Attachment(); 
} 
if(accountId != null)
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully')); 
   return new Pagereference('/'+accountId );
return  new Pagereference('/001');
} 
}

Page

<apex:page standardController="Account" extensions="FileUploadController">
<apex:sectionHeader title="Account" subtitle="Account Attachments"/>

<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Upload a File">

<apex:pageBlockButtons >
<apex:commandButton action="{!upload}" value="Attach"/> 
</apex:pageBlockButtons>

<apex:pageBlockSection showHeader="false" columns="2" id="block1">


<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
</apex:pageBlockSectionItem>


</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:page>
3
  • In the above code when i click the attach button file uploaded and redirect to account detail page.Now i want is to display a pop up that file uploaded sucessfully then redirect to the account detail page.
    – karuna
    Dec 14 '16 at 5:05
  • why do you need two return statements in upload()
    – Reshma
    Dec 14 '16 at 6:06
  • But the above code is working fine it is redirect in the account detail page when click on attach button.The problem is i want to have an alert message before redirecting into Account detail page
    – karuna
    Dec 14 '16 at 6:14
0

Try this.

<apex:commandButton action="{!upload}" value="Attach" onclick="alert('File uploaded sucessfully.');"/> 
1
  • 2
    @karuna How you are sure that your file uploaded successfullly? Onclick event occurs, if the user clicks the command button. It has nothing to do with success or failure?
    – Saroj Bera
    Dec 14 '16 at 6:59
0

One approach would be to use a non-transient flag variable in Apex and check it's value in JavaScript when page is auto reloaded once upload is completed or you can use document.id != null when page is auto reloaded.

e.g.

<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Upload a File">

<apex:pageBlockButtons >
<apex:commandButton action="{!upload}" value="Attach"/> 
</apex:pageBlockButtons>

<apex:pageBlockSection showHeader="false" columns="2" id="block1">


<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
</apex:pageBlockSectionItem>


</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>

<script>
   if('{!document.id}' != 'null') alert('File uploaded successfully');
</script>
</apex:page>
2
  • i did'nt understand
    – karuna
    Dec 14 '16 at 5:57
  • @karuna, since you cannot use ajax function because of <apex:inputFile> component therefore page is going to reload anyway and at that time the script will display the alert if file upload was successful.
    – LakhanP
    Dec 14 '16 at 6:40

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.