Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi team I am implementing the uploading the text file and preview the uploaded file and once we upload file if we need to search the text inside the uploaded file how can i achieve it in angular 2+or any angular version

What I have tried:

I tried the
 openTxt(event) {

var file = event.target.files[0];
    let reader = new FileReader();
    reader.onload = (e: any) => {
      // The file's text will be printed here
      this.content = e.target.result;
    };

    reader.readAsText(file);

  }
Posted
Updated 25-Jan-22 0:08am

1 solution

Try the following to search text inside your input file:

var file = event.target.files[0];
let reader = new FileReader();
reader.onload = (e: any) => {
  // The file's text will be printed here
  
	$scope.fileContent = Reader.result;
	$scope.fileName = document.getElementById("fileinput").files[0].name;
	$scope.fileSize = document.getElementById("fileinput").files[0].size;
	
	if(data.includes('Search String')){
		console.log(data)
  }
  
  this.content = e.target.result;
};

reader.readAsText(file);
 
Share this answer
 
Comments
sravani n 2022 25-Jan-22 8:33am    
actually i am using angular2+ version and actually once i upload the document i need to search the text and highlight the part is it possible?

and i think based on your answer i can search any text inside the file right?
M Imran Ansari 25-Jan-22 8:52am    
You can get the uploaded file as per above code.
Correct. Pass your search text through includes method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900