botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] Show images from Database in Hero Cards

Open neetgupta7 opened this issue 9 years ago • 7 comments

Hello Based on my business logic I want to fetch an image from database and use it on my Hero Card. But hero card accepts the image url as the input. How can I achieve this without url or when an image has to be fetched dynamically from a database or some other source.

neetgupta7 avatar Jun 28 '17 06:06 neetgupta7

@neetgupta7 Are you using .NET or Node SDK?

nwhitmont avatar Jun 28 '17 14:06 nwhitmont

.net

neetgupta7 avatar Jun 28 '17 14:06 neetgupta7

@neetgupta7 This repository is for questions about BotBuidler SDK. If you'd like to learn about working with databases, try Stack Overflow or Google search.

nwhitmont avatar Jun 28 '17 15:06 nwhitmont

Not sure if my description was not clear enough. Hero cards accepts https URL for images, what should I do if my image is not hosted in a website but in present in a database. How do I show them.

neetgupta7 avatar Jun 28 '17 15:06 neetgupta7

Hi @neetgupta7

You can add another web api controller to the bot that returns the images, and use that url. Here is an example of how to return an image from a database:

[HttpGet]
public HttpResponseMessage Get(Guid id)
{
    HttpResponseMessage result = null;
    try
    {
        var artwork = _repository.GetArtwork(id);
        if (artwork == null)
        {
            result = Request.CreateResponse(HttpStatusCode.Gone);
        }
        else
        {
            // sendo file to client
            byte[] bytes = artwork.artworkMeta ;
            result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(bytes);
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = artwork.artworkName;
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        } 

        return result;
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.Gone);
    }
}

from: https://stackoverflow.com/questions/41165240/return-image-from-db-and-display-with-net-api-and-angularjs

I hope this helps.


The Microsoft Bot Framework team prefers that how to questions be submitted on Stack Overflow. The official Bot Framework Github repo  is the preferred platform for submitting bug fixes and feature requests.  

EricDahlvang avatar Jul 05 '17 17:07 EricDahlvang

var bytes = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Content/image.jpg")); string imageData = Convert.ToBase64String(bytes, 0, bytes.Length); HeroCard heroCard = new HeroCard() { Title = Name, Subtitle = Address, Images = new List() { new CardImage() { Url = "data:image/png;base64,"+ imageData } } }

I had the same issue and after having a look at the SendAttachmentBot example (available on github by microsoft) came to know that you can simply put image data in the "Url" parameter and it is displayed just fine.

vish-vas avatar Aug 07 '17 06:08 vish-vas

This issue is closed.

If you have a question or problem, please open a new issue.

nwhitmont avatar Aug 08 '17 01:08 nwhitmont