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
ssousanssousan 

Is it possible to read content of a text file with VisualForce

Hi Everyone,

 

I have a text file that is uploaded by the user,

Using the Document class,

 

How can i read the content of that text file,

ex: Name, address, email, ...etc,

 

I want to save those paramters to an object i have created,

 

Is it possible to open and read an uploaded text file with VisualForce or Apex?

 

Thanks 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
If thsi is the case, the you can convert your blob into string value. Split this based on the newline string.split('\r\n') character.
You can store the result in list string array and loop through it to check whether the line start with "Name", "address" etc.

You can use class local varibales to store this data and display on VF page

All Answers

dphilldphill

I don't believe you can do this with Visualforce.  In Apex, you should be able to load the text document as a blob then use the toString method on it.

 

Blob fileBody = aFile.Body;
String fileText = fileBody.toString();

 I'm not 100% sure this is the complete correct syntax but should be a good start.

ssousanssousan

Ok, please correct me if I’m wrong,

 

I can create an Apex Class that reads the text document,

And pass the document from The VF to the class,

And then the class will pass back the information I want,

 

Is that correct?

 

Best

dphilldphill
Hmmm? VF isn't involved anywhere in there. Do you need to pass it to your VF page for some reason?
ssousanssousan

Well the text file is uploaded using VF,

But instaed of saving the text file,

i want to read information from the text file,

And only save that information to an object,

 

Is this possible?

 

 

Best 

Bhawani SharmaBhawani Sharma
In what format data will be on your VF page? Are these values separated by newline character or will always be in the concrete format?
ssousanssousan

The document might differ,

But mainaily i would be looking at specific locations,

And they are seprated by line,

 

Best

Bhawani SharmaBhawani Sharma
If thsi is the case, the you can convert your blob into string value. Split this based on the newline string.split('\r\n') character.
You can store the result in list string array and loop through it to check whether the line start with "Name", "address" etc.

You can use class local varibales to store this data and display on VF page
This was selected as the best answer
ssousanssousan

Great, thanks,

I appreciate everyones help,

 

Best 

ssousanssousan

Do you have a url or an example,

That does what you just described?

 

Thanks