.NET Core Web API - how to return search result images to the client

I'm writing a .NET Core Web API and am trying to return search results with their associated images. The images are stored in directories. Can I return the view model with the images, or do I return the view model, then make a call to retrieve each image.

Any examples would be appreciated. I have found lots of examples of uploading files from client to server, but can't find the reverse.

Posted Sep 1, 2020, 6:59 AM UTC

38
tdubeau•

Personally I'd return the URL to the location of the image render it using an <img> tag in your HTML.

Make sure your images are stored in a location accessible by the client browser.

8
Jesse2014•

This. Maybe even store the images on a separate server to the main app - like a static asset server.

3
rawpineappleOP•

Thanks.

1
rawpineappleOP•

Thanks. I'll look into it.

1
GreenEyedCat96•

Can I return the view model with the images, or do I return the view model, then make a call to retrieve each image.

Anyway, it's better to return it in one call, because it's faster. What do you mean "return images"? Return image's urls or return binary data?

1
rawpineappleOP•

Return the binary data. I added a IFormFile property to my view model, and was trying to read my file into it somehow???? Processing files is pretty new to me so I could be on the wrong track.

1
GreenEyedCat96•

I added a IFormFile property to my view model

You shoud create some class, which represent your model of file, with your properties (or use simple class like string or byte[] for file, if it's enaugh for you). IFormFile is interface used by asp.net to upload files, you should create your own view model instead.

What would you want to do with that file? Choosing between embedding files content in your view model or get them by next api call depends on what would you want to do with them next, their sizes, etc..

2
rawpineappleOP•

Thanks for your help.

I want to display thumbnails in the search results. I'll add a byte[] property to my view model.

1