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
LavanyaLavanya 

how to create a Vf page to upload a data file

Hi All, I am trying to create VF page which has two button "search" and "upload". when i click the "search" button i want it to search the data file stored in mt system. Then i click "upload" which is to upload a data file stored in our system . It is same as the upload files option in gmail. kindly help me to resolve this issue. Thanks , Regards, lavanya.

Srinath TeramSrinath Teram

HI Lavanya,

 

You could use <apex:inputFile> tag to fetch the standarad file-select popup. Further you might want to write the code for inserting the record to "Attachments" object in Salesforce. Let me know in case you need more clarification.

 

Thanks,

Srinath T

Devendra@SFDCDevendra@SFDC

Hi,

 

 

You can go through the below code

<apex:page standardController="CustomObj__c" extensions="FileUploadController" 
	tabStyle="CustomObj__c" showHeader="true">
	
	<apex:pageBlockSection title="Select a file to be uploaded" collapsible="false"/>

	<apex:outputPanel id="panel_Upload">
		<apex:form id="form_Upload">
			<apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/>
    			<br/>
			<apex:commandButton id="button_Upload" value="Upload" action="{!processUpload}" />
		</apex:form>
	</apex:outputPanel>
	<br/>
</apex:page>


public class FileUploadController 
{
	private String fileName;
	private Integer fileSize;
	private Blob fileBody;

	public FileUploadController(ApexPages.StandardController controller){}

	public FileUploadController(){}


	public String getFileName()
	{
		return this.fileName;
	}
	
	public void setFileName(String fileName)
	{
		this.fileName = fileName;
	}
	
	public Blob getFileBody()
	{
		return this.fileBody;
	}
	
	public void setFileBody(Blob fileBody)
	{
		this.fileBody = fileBody;
		setFileSize(this.fileBody.size());
	}

	public Integer getFileSize()
	{
		return this.fileSize;
	}

	public void setFileSize(Integer fileSize)
	{
		this.fileSize = fileSize;
	}
	

	private Database.SaveResult insertCustomObject()
	{
		CustomObj__c custObj = new CustomObj__c();

		// fill out cust obj fields

		return Database.insert(custObj);
	}

	private Database.SaveResult insertAttachment(Id parentId)
	{
		Database.SaveResult result;
				
		Attachment attachment = new Attachment();
		attachment.Body = this.fileBody;
		attachment.Name = this.fileName;
		attachment.ParentId = parentId;
		
		result = Database.insert(attachment);

		fileBody = Blob.valueOf('  ');
		return result;
	}

public PageReference processUpload()
	{
		try
		{
			Database.SaveResult result = insertCustomObj();
			
			if (result == null || !result.isSuccess())
			{
				return null;
			}
	
				
			result = insertAttachment(result.getId());

			if (result == null || !result.isSuccess())
			{
				return null;
			}

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

		return returnUrl();
	}
}

 

Thanks,

Devendra

LavanyaLavanya

Hi Devendra, thanks for the reply.
I am getting this error.
Error: Compile Error: Non-void method might not return a value or might have statement after a return statement. at line 82 column 13

 

code:

public class FileUploadController
{
private String fileName;
private Integer fileSize;
private Blob fileBody;

public FileUploadController(ApexPages.StandardController controller){}

public FileUploadController(){}


public String getFileName()
{
return this.fileName;
}

public void setFileName(String fileName)
{
this.fileName = fileName;
}

public Blob getFileBody()
{
return this.fileBody;
}

public void setFileBody(Blob fileBody)
{
this.fileBody = fileBody;
setFileSize(this.fileBody.size());
}

public Integer getFileSize()
{
return this.fileSize;
}

public void setFileSize(Integer fileSize)
{
this.fileSize = fileSize;
}

private Database.SaveResult insertUpload()
{
Upload__c uploadObj = new Upload__c();

// fill out cust obj fields

return Database.insert(uploadObj);
}

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

Attachment attachment = new Attachment();
attachment.Body = this.fileBody;
attachment.Name = this.fileName;
attachment.ParentId = parentId;

result = Database.insert(attachment);

fileBody = Blob.valueOf(' ');
return result;
}

public PageReference processUpload()
{
try
{
Database.SaveResult result = insertUpload();

if (result == null || !result.isSuccess())
{
return null;
}


result = insertAttachment(result.getId());

if (result == null || !result.isSuccess())
{
return null;
}

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

//return Url();
}
}

 

Kindly any one tell how to resolve this.

Thanks,

Regards,

Lavanya.