2

I have a nodejs app running on localhost:3000, I uploaded images using multer, so they are in ./uploads/ folder. In the Angular app running on locahost:4200 I want to retrieve those images.

in my for loop:

<tr *ngFor="let data of empData;let i=index">
    <td>{{i+1}}</td>
    <td>{{data.emp_name}}</td>
    <td><img src="./uploads/{{data.emp_image}}"></td>
</tr>

I have all data in following result variable:

this.emp.viewData().subscribe(
  res => {
    this.empData = res;
  },
  err => {
    console.log(err);
  },
)
2
  • you have to append the folder or bucket path before the data value to access the image like http(s)://localhost:8080/src/images/data.value Commented Jul 8, 2019 at 13:03
  • Can u explain more or give a example Commented Jul 8, 2019 at 13:19

2 Answers 2

2

In addition to the answer above by Software Person, ensure you include this to you node application to enable you access the uploads directory in your nodejs application.

const express   = require('express');
const app       = express();

app.use(express.static('uploads'));

Happy Coding.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, it helped me a lot.
1

You could include your full url like

<tr *ngFor="let data of empData;let i=index">
    <td>{{i+1}}</td>
    <td>{{data.emp_name}}</td>
    <td><img src="http(s)://localhost:3000/uploads/{{data.emp_image}}"></td>
</tr>

Or even better you could define a variable in component and use it like this:

<tr *ngFor="let data of empData;let i=index">
    <td>{{i+1}}</td>
    <td>{{data.emp_name}}</td>
    <td><img src="{{SERVER_UPLOAD_DIR}}/{{data.emp_image}}"></td>
</tr>

4 Comments

Could you add the code of your component and try access the url in your browser if you can access it that way
I have access the url through browser it gives the error as follows: Cannot GET /uploads/emp_img-1562410462956.png
A 404 I guess, Which means you have uploaded it to a non-public folder. Could you provide your nodejs implementation and your folder structure. So I can give you a solution (just edit your original)
Thank u sir, I have fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.