Search code examples
angularimageaspnetboilerplate.net-core-3.1

Get Image(Binary Data) from database with .Net Core and Angular 8


I have images stored in database as Binary Data ,I wanna return this image for client to display them but the problem is code that I've written return json data not the image, how could i return the image as its for the client in .NET Core 3.1 with Angular 8

//note document is the column of my image Here is my code :

public async Task<dynamic> GetImage(int id)
        {

            string imageBase64Data =Convert.ToBase64String(_repository.Get(id).document);
            string imageDataURL =string.Format("data:image/jpg;base64,{0}",imageBase64Data);
           
            return imageDataURL;
        }

I'm using ABP framework and my class is ApplicationService Base Class


Solution

  • I found it from Server Side (.Net Core ) I've used FileStreamResult this will return a link that enable you to donwload the file or you can use FileContentResult to return the file directly

    //myImage.document is a byte[]
    public async Task<FileStreamResult> GetDoc(int id)
     {
                var myImage = await _repository.GetAsync(id);
                var stream = new MemoryStream(myImage.document);
                
                return new FileStreamResult(stream, "image/jpeg" /*chnage this based on your image type */ )
                {
                    FileDownloadName = "test.jpeg"
                };
               
            }