0

So I have a input file in my visualforce page. The conditions that I have on the file are them not being more than 10MB and of type doc, docx or pdf. The issue I'm facing is that I lose the file already attached whenever I click the cancel on the file dialog or if I chose a wrong file of different extension. But the thing is I would want to keep the old value of the file in that case.

<apex:inputFile id="cvinput" onchange="ValidateSize(this)" styleClass="slds-file-selector__input slds-assistive-text" value="{!document}" accept=".doc,.docx,.pdf" filename="{!fileName}" contentType="{!contentType}" filesize="1000" size="50" />

And I'm not sure how not to lose the old values in those cases,here's the script I use in the page:

 function ValidateSize(file) {
        if (file.files[0]==null){
            document.getElementById("fileName").innerHTML ='';
            //I want to keep the old value
        }

        else{

        var f = file.files[0];
        var FileSize = file.files[0].size / 1024 / 1024; // in MB
        var fname= file.files[0].name; 
        var ext= fname.split('.').pop();
        if( FileSize > 10 || !(ext == "pdf" ||  ext=='doc' || ext=='docx'   ))
        {
            file.value = null;
            window.alert('Seuls les fichiers de type doc, docx ou pdf ne dépassant pas 10MB sont acceptés');
            //I want to keep the old value
            return false;
        }

       else {
            var r = new FileReader();
            r.onload = function(e) {
                document.getElementById("fileName").innerHTML = f.name;
            }
            r.readAsText(f);
            $("[id$=removecv]").css('display', 'inline');
        }
    }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.