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}`);
}
}
);
http.get('files/1.txt')-- no initial slash) Or placing the files in theassetsfolder, the content of which - I believe - is copied as is at the root of the site.ng serveit only processes .ts, .html and .css files. Static assets like .txt files do not belong in your code and they are dropped byng serve.