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

Upload and view


my requirement is to upload a file and once the file is uploaded,

i must be able to view the content of the uploaded file.could

anyone help me with an example or a sample code how could i

achieve this...


Thank you.




Use This code in Visualforce page:


<apex:inputFile value="{!contentFile}" filename="{!FileName}" />



Use this code on submit in Apex Class:


Public Blob contentFile{get;set;}

Public Void Submit()


String FinalResult=contentFile.toString();



If u dont want to convert it into String then Store file into Doucument Object record


If any query then ask 

 public Blob contentFile{get;set;}

Public Void Submit(){String FinalResult=contentFile.toString();}


could u pls let me know the sample code

As i am new to visualForce,so could u 

send me the code so i could achieve my task..



Thank you in advance....



when i  upload a specefic file

i should he able to get the uploaded

content from the controller.

could anybody help me witht he sample


Yhank You..


Here is a small example, let me know if that helps you. 



Below is the code for the VF Page :


<apex:page controller="UploadAndView">
	<apex:form >
		<apex:PageBlock Title="Upload your documents" id="pb1">
			Upload a file: <apex:inputFile value="{!doc}" filename="{!docName}"/>
			<apex:commandButton value="Upload and View" action="{!saveDocument}" />

 and this is the controller for the VF page :


public with sharing class UploadAndView 
	public Blob doc{get;set;} // this is for the document you are going to upload
	public String docName {get;set;} // this is for the document name
	public UploadAndView()
	public pageReference saveDocument()
		if(doc != null) // if the document is not null, ie, you have chosen a file from the page
			Document d = new Document();
			d.Body = doc;
			d.Name = docName;
			d.FolderId = userinfo.getUserId(); // this saves the document in the user's personal documents folder
			d.IsPublic = true;
			d.IsInternalUseOnly = false;
			insert d;
			String baseURL = String.valueOf(URL.getSalesforceBaseUrl());
			Pagereference pr = new PageReference('/servlet/servlet.FileDownload?file='+d.Id);
			return pr;
			return null;

 Let me know if you have any queries!

Richa KRicha K

I think I have already posted you the solution.


This will give you the uploaded fies list. 

In Class :

public list<Document> lstDoc = new list<Document>();
public list<Document> getNewFun()
      for(Document doc : [select FolderId, Name, Url from Document])
      return lstDoc;


In Page :

<apex:pageBlock title="Documents">

    <apex:pageBlockTable value="{!NewFun}" var="aDoc" width="100%" >
     <apex:column >
      <apex:facet name="header"><b>Document Name</b></apex:facet>
     <apex:column >
     <apex:outputLink value="{!aDoc.Id}">{!aDoc.Id}</apex:outputLink> 
       <apex:column >
      <apex:facet name="header"><b>Document Folder ID</b></apex:facet>



 This will show you the document list alongwith the links to the documents.

Hope this will help!



this is working perfectly , but u have used document ,

but i dont want that , and the content must be saved

as string , could u give me any other example for this



Thank You