I have a .txt file in my Angular 4 project directory and I want to read its content. How to do it ? Below is the code which I employed.
The file is in 'files' folder which is inside the 'app' folder.
The component where i have HTTPClient code is in 'httpclient' folder which is inside 'app' folder.
Meaning 'files' folder and 'httpclient' folder are children.
The code is shown below. It not working as i m getting 404 error - 'GET http://localhost:4200/files/1.txt 404 (Not Found)'
this.http.get('/files/1.txt').subscribe(data => {
console.log(data);
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly.
console.log('An error occurred:', err.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
}
}
);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…