0

I've create a vf page; and file upload functionality when i select the file and click on upload resume button, the file will attach as required it works fine.

But i dont want upload resume button.
I want when i click on choose file button and after file selected, click on open file and then file should be uploaded, my controller method should call at this time

How to call my controller method in such situation to upload without clicking on button

screenshot

enter image description here

My VF page Code

  <apex:page controller="VF_Introduce_Me1" sidebar="false" >
  <apex:form>
  <td class="tdlabel">Resumé</td> <!-- Johnny added this for resume upload. Issue: Since Query is only run once and there's no refresh, the recently uploaded resume does not apprear-->

                    <apex:outputPanel rendered="{!IF(ISBLANK(hunt.Resume_Lookup__c),TRUE,FALSE)}">
                        <td class="tdvalue"><apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}" /> 

                        <apex:commandButton id="uploadBtn" value="Upload Resumé" action="{!processUpload}" />  
                       <apex:outputField value="  {!hunt.Resume_Required__c}" /></td>
                    </apex:outputPanel>

 </apex:form>
</apex:page>

Controller:

 public with sharing class VF_Introduce_Me1 {

 public PageReference processUpload() {
    try {
        Database.SaveResult customAttachmentResult = saveResume();

        if ( customAttachmentResult == null || !customAttachmentResult.isSuccess() ) {
            ApexPages.AddMessage( new ApexPages.Message( ApexPages.Severity.ERROR, 'Could not upload Resumé' ) );
            return null;
        }

        Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());

        if (attachmentResult == null || !attachmentResult.isSuccess()) {
            ApexPages.AddMessage(new ApexPages.Message( ApexPages.Severity.ERROR, 'Could not save Resumé' ) );            
            return null;
        } else {
            // update the custom attachment record with some attachment info
            Resume__c customAttachment = [ SELECT Id FROM Resume__c WHERE id =: customAttachmentResult.getId() ];

            customAttachment.Last_Upload_Id__c = attachmentResult.getId();
            customAttachment.Last_Upload_Name__c = this.fileName;

            String premonth;
            IF( system.today().month() < 10 ) premonth = '0';
            ELSE premonth = '';
            String preday;
            IF( system.today().day() < 10 ) preday = '0';
            ELSE preday = '';
            customAttachment.name = hunt.Lion__r.FirstName + ' ' + hunt.Lion__r.LastName  + ' CV – ' + system.today().year() + premonth + system.today().month() + preday + system.today().day();

            String try_type = this.fileName.substring( this.fileName.length() - 3,  this.fileName.length() ).toUpperCase();

            IF( try_type == 'OCX' ) customAttachment.File_Type__c = 'DOC';
            ELSE IF( try_type == 'PDF' || try_type == 'DOC' || try_type == 'PNG' ) customAttachment.File_Type__c = try_type;
            ELSE customAttachment.File_Type__c = '???';

            customAttachment.Resume__c = attachmentResult.getId();
            UPDATE customAttachment;

            hunt.Resume_Lookup__c = customAttachment.Id;
            UPDATE hunt;
            fetchhuntrecord();

        }

    } catch (Exception e) {
        ApexPages.AddMessages(e);
        return null;
    }
    return null;
    }

    private Database.SaveResult saveResume() {
    Resume__c obj = NEW Resume__c();
    obj.Hunt__c = hunt.Id; 
    obj.Last_Uploaded__c = system.Now();
    return Database.insert(obj);
}

private Database.SaveResult saveStandardAttachment(Id parentId) {
    Database.SaveResult result;

    Attachment attachment = NEW Attachment();
    attachment.body = this.fileBody;
    attachment.name = this.fileName;
    attachment.parentId = parentId;
    // insert the attachment
    result = Database.insert(attachment);

    // reset the file for the view state
    fileBody = blob.valueOf(' ');
    return result;
}

}
0

You can do this by using <apex:actionFunction. Call the controller action with the help of <apex:actionFunction when there is a onchange event occurs for <apex:inputFile.

VF Page

<apex:page standardController="Document" extensions="documentExt">
    <apex:messages />
    <apex:form id="theForm">
      <apex:pageBlock>
          <apex:pageBlockSection>
              <apex:inputFile value="{!document.body}" filename="{!document.name}" onchange="uploadfile();" />
          </apex:pageBlockSection>
       </apex:pageBlock>
        <apex:actionFunction action="{!save}" name="uploadfile" />
    </apex:form>
</apex:page>

Controller

public class documentExt {
    public documentExt(ApexPages.StandardController controller) {
        Document d = (Document) controller.getRecord();
        d.folderid = UserInfo.getUserId(); //this puts it in My Personal Documents
    }                 
}
|improve this answer|||||

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.