简体   繁体   中英

Angular 5: How do I access image files outside the app or assets folder?

I have app where user can upload images. I read that I need to upload these files outside the app or assets folder as those folders get compiled. My app folder structure looks like this: 在此处输入图片说明

User files are supposed to go into media folder. Right now I have there example file media/user/default_dp.jpg , but how do I access to this file in component template? Writing just

`<div class="my-dp dp rounded-circle" [ngStyle]="{'background-image':url(./media/user/default_dp.jpg)'}">`

does not do the trick.

You should not (in this case) consider those as a part of your app. Consider those media files as things that come from backend, instead.

The uploaded files are completely separated form your Angular app. Your "server" folder will likely have something like that (or better, move it completely outside of the sources). Your server needs to, on upload, put the files somewhere publically accessible, or provide an endpoint where it will serve those files. And whatever this backend server gives you is gonna be where you fetch it from.

你能试一下吗 :

<div class="my-dp dp rounded-circle" [ngStyle]="{'background-image': './media/user/default_dp.jpg'}"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM