0

I have simple action: save image as attachment. I do it by actionFunction and I don't need to reload page when I click SAVE button. And when I add attr reRender in actionFunction statement then file from inputFile doesn't exist. When I delete reRender attachment saves but page reload. What can I do for save file from inputFile and don't reload page. I added actionRegion because apex:inputFile can't be used in conjunction with actionFunction. I repeat part of my comment here: if you can't help just do nothing. Do not try to prove that you are very clever and know something when it is not. I will not explain difference between my question and another so if you don't understand it then you'll not understand difference between cow and frog.

This is my code.

VF page:

<div id="addScheduleBlock">
    <apex:actionRegion>
        <apex:actionFunction name="callSaveSchedule" action="{!saveSchedule}" status="initScheduleBlock"/>
        <apex:outputLabel value="Date"  for="sheduledate"/>
        <apex:input type="date" value="{!scheduleDateForSave}" required="true" id="sheduledate"/>
        <input type="button" value="Save" disabled="true" id="saveScheduleButton"/>
    </apex:actionRegion>
    <apex:inputFile value="{!attachedFile.body}" filename="{!attachedFile.Name}" id="sheduleImg" accept="image/*"/>
</div>

JS:

function saveSchedule(){
  $("#saveScheduleButton").on( "click", function() {
    if (confirm('Are you shure?')) {
      callSaveSchedule();
    } else {
      event.preventDefault();
      event.stopPropagation();
      return false;
    }
  });
}

Apex Controller:

public PageReference saveSchedule() {
    List<Schedule__c> scheduleToDelete = [SELECT Id FROM Schedule__c WHERE ScheduleDate__c =: scheduleDateForSave];
    delete scheduleToDelete;
    schedule.ScheduleDate__c = scheduleDateForSave;
    insert schedule;
    attachedFile.ParentId = schedule.Id;
    insert attachedFile;  
    schedule = new Schedule__c();
    attachedFile = New Attachment();
    return null;
}

marked as duplicate by Tushar Sharma, Eric, Ratan Paul, Vigneshwaran G, Sebastian Kessel Oct 10 '16 at 15:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • apex:inputFile can't be used in conjunction with an action component when i'm trying to delete a row – it's not the same question. I know that I can't use reRender with conjunction to inputFile. – Dmitry Oct 10 '16 at 10:32
  • Is there a particular reason you want the page not to reload? – Guy Clairbois Oct 10 '16 at 10:55
  • Yes. When page reloads I lose all opened and loaded information, loading start page and I must to do some actions again. – Dmitry Oct 10 '16 at 10:57
  • I'm not sure why this was marked as a duplicate, but I think the answer OP wants is in a different question here. Basically, you'll need to use an AJAX call to update the data without a post back, Visualforce Remoting may work but the REST API will allow you to upload larger files. – IllusiveBrian Oct 10 '16 at 15:23
  • I think when you click duplicated you didn't read question at all. If you can't help don't need to let on that you very smart and it's very easy for you. Just do nothing or give useful advice. Thank you IllusiveBrian. I know this way and will do so. But I thought that someone can help me to solve problem by using default SF tools. – Dmitry Oct 10 '16 at 16:47