0

I created a visualforce page with a button to download a document, this work fine, but after download the document I want to delete the file without to click in another button, this is the code I am using:

<apex:page standardController="npe03__Recurring_Donation__c" extensions="ReporteCompromisosExportacionController" action="{!getFile}" tabStyle="Reporte_Compromisos__tab">    
    <apex:form >
        <apex:actionFunction name="download" action="{!downloadFile}"/>
        <apex:actionFunction name="deletef" action="{!deleteFile}" rerender="name"/>
        <apex:pageBlock id="page">
            <apex:pageBlockSection columns="1" title="Descargar Archivo de Cobranza" collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:image url="{!$Resource.excelIcon}" width="30" height="30"/>
                    <apex:outputText id="name" value="Archivo {!fileName}"/>                    
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:CommandButton value="Descargar Archivo" action="{!downloadFile}" oncomplete="deletef();" rerender="name"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Extension

public void getFile(){
        String idDocument = Apexpages.currentPage().getParameters().get('doc');
        List<Document> docs = [SELECT Name FROM Document WHERE Id =: idDocument];
        if(docs.size()>0){
            fileName = docs.get(0).Name;
        }       
    }

    public String downloadFile(){
        String idDocument = Apexpages.currentPage().getParameters().get('doc');
        String URL = '/servlet/servlet.FileDownload?file=' + idDocument;
        return URL;     
    }

    public void deleteFile(){
        String idDocument = Apexpages.currentPage().getParameters().get('doc');
        Document d = new Document();
        d.Id = idDocument;
        delete d;
    }
}

The oncomple method doesn't work, can some one help me please?

3
  • What does the `{!downloadFile} do? It redirect to document url?
    – Ashwani
    Commented Jan 11, 2017 at 18:23
  • 2
    Please share your extension code.
    – Adrian Larson
    Commented Jan 11, 2017 at 18:25
  • Ashwani downloadFile return a String with the URL to the file, AdrianLarson, I edit the question with the extension code. Commented Jan 11, 2017 at 21:03

1 Answer 1

2

You should not delete the document until and unless you make sure that document is downloaded successfully.

You current approach can delete the document even it is in middle of download. Because as soon as String (url) is returned, it will call the oncomplete and delete the document. This will halt the download immediately. Assuming the case if oncomplete works

Instead, you just make the idDocument to null so, if user click on the button it won't be able to get the file as ID is null.

Have some asynchronous process lets say scheduled Batch to delete the document after 5 minutes or the considerable time when you are sure that document would be downloaded upto that time.

OnComplete doesn't work because method downloadFile must return either PageReference type or void. null return would also acceptable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .