function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cyril CatonCyril Caton 

Loading document via a pop-up window

Hi everyone,

 

I'm trying to allow a user to upload an image in a specific directory, and keep a reference to that image in a custom setting parameter. So far, nothing too complicated.

 

To do that, I display the current image, I make it clickable. On click, a pop-up window shows up, with the classic "select file" button that helps you select a file from your hard-drive, and a validate button loading the image and updating the custom setting parameter.

 

My issue: the image loading only works ... every second time I try to do it! Basically, I open the pop-up window, select a file, validate: nothing happens and the pop-up window stays open. Select the same file, validate: here you go, my file is getting uploaded and parameter gets updated. 

 

Here is the code. Note: I left the debug message in the controller. First time I try to upload the image, I see 'body is null' in the logs. Second time, everything works fine.

 

Thanks for your help!

 

VF page

  
<apex:actionFunction name="showPopUp" action="{!showPopup}" rerender="popup" />

<apex:image id="logo" value="{!myTemplate.URL_Logo__c}" styleClass="logo" alt="Logo" onclick="showPopUp();return false;" /> <!-- Pop-up allowing to select a new file --> <apex:outputPanel id="popup"> <apex:outputPanel styleClass="customPopup" layout="block" rendered="{!displayPopUp}"> <h2>Sélectionner votre logo</h2> <br /><apex:inputFile value="{!document.body}" filename="{!document.name}"/> <apex:commandButton value="Valider" action="{!doUpload}" /> </apex:outputPanel> </apex:outputPanel>

 

Custom Controller

public Document document
{
	get
	{
		if(document==null)
		{
			document = new Document();
		}
		return document;
	}
	set;
}

public Raison_Sociale__c myRaisonSociale {get;set;}

public boolean displayPopup {get; set;}

public void closePopup() {
	displayPopup = false;
}

public void showPopup() {
	displayPopup = true;
} 

(...)

//Uploading an image in a specific Document directory
public PageReference doUpload() 
{
	if (document.body != null) {
		system.debug('body is not null');
		if (document.name != null) {
			system.debug('name is not null');
			Document d = document;
			//Looking for the Document directory named Logos Factures               
			Folder[] folders = [select id from Folder where name='Logos_Facture'];
			if (folders.isEmpty()) {
				system.debug('Did not find Logos_Factures directory');
				d.folderid = UserInfo.getUserId(); //store in Personal Documents
			} else {
				system.debug('found Logos_Factures directory');
				d.folderId = folders[0].id;
			}
			//Making the document available to everyone
			d.IsPublic = true;
			try {
				insert d;
				system.debug('insert doc ok');
				system.debug('BEFORE: template url logo = ' + myTemplate.URL_Logo__c);
				myTemplate.URL_Logo__c = DocumentUtil.getInstance().getURL(d);
				system.debug('AFTER: template url logo = ' + myTemplate.URL_Logo__c);
				update myTemplate;
				system.debug('update url logo ok');
			} catch (Exception e) {
				ApexPages.addMessages(e);
				system.debug('Problem inserting the document or updating the template: ' + e);
			}  

			d.body = null;
			
			//Adding redirection if using the pop-up module
			//so the page gets refreshed since rerender is not supported
			PageReference curPage = ApexPages.currentPage();
			curPage.setRedirect(true);
			return curPage;
		} else {
			system.debug('name is null');
		}
	} else {
		system.debug('body is null');
	}

	return null;
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Cyril CatonCyril Caton

Since I found the solution, I thought it was worth sharing it here.

The problem was with the showPopUp() action.

 

In my initial implemnetation, it was defined like this:

 

<apex:actionFunction name="showPopUp" action="{!showPopup}" rerender="popup" />

 

Turns out the rerender option was too much, and was corrupting my controller intialization (body and name variables were not getting set on pop-up init). Revoved it, and image loading now works like a charm!