ASP.NET Base64图像源无法正常显示。

时间:2018-05-08 06:02:29

标签: html asp.net image base64

我通过Lambda函数从Azure活动目录中获取Base64字符串。这表示用户个人资料图片。然后我尝试在ASP.NET页面中显示它。它不起作用。

这是ASP.NET(引用的HTML对象是ASP文字)

            using (var client = new AmazonLambdaClient(Amazon.RegionEndpoint.APSoutheast2))
            {
                var request = new InvokeRequest
                {
                    FunctionName = ConfigurationManager.AppSettings["lambdaArnPrefix"] + "lambda-wlyw-GetAzureAdUserThumbnail",
                    Payload = "\"" + Context.User.Identity.Name.ToString() + "\"",
                    InvocationType = InvocationType.RequestResponse

                };
                var response = client.Invoke(request);

                string result;
                using (var sr = new StreamReader(response.Payload))
                {
                    result = sr.ReadToEnd();
                }

                dynamic userThumbnail = JsonConvert.DeserializeObject(result);
                photo.Text = "<img src='data:image/jpg;base64," + userThumbnail.base64Image + "' width='100' height='100'/>";

然而,该页面显示了损坏的图像图标。如果我查看源代码,对我来说似乎没问题?如果我使用在线转换器,base64字符串会解码为图像:

  <div class="panel-group">
                                            <div class="panel panel-primary">
                                                <div class="panel-heading">James Matson - Excellence<i class="fa fa-certificate pull-right"></i></div>
                                                <img src='data:image/jpg;base64,eyJvZGF0YS5lcnJvciI6eyJjb2RlIjoiUmVxdWVzdF9SZXNvdXJjZU5vdEZvdW5kIiwibWVzc2FnZSI6eyJsYW5nIjoiZW4iLCJ2YWx1ZSI6IlJlc291cmNlICd0aHVtYm5haWxQaG90bycgZG9lcyBub3QgZXhpc3Qgb3Igb25lIG9mIGl0cyBxdWVyaWVkIHJlZmVyZW5jZS1wcm9wZXJ0eSBvYmplY3RzIGFyZSBub3QgcHJlc2VudC4ifX19' width='100' height='100'/>

1 个答案:

答案 0 :(得分:0)

找出问题 - 道歉。事实证明我的缓存凭据不存在,这意味着Lambda函数返回的Base64字符串实际上不是图像,而是来自Azure AD的错误代码,即:

{"odata.error":{"code":"Request_ResourceNotFound","message":{"lang":"en","value":"Resource 'thumbnailPhoto' does not exist or one of its queried reference-property objects are not present."}}}

一旦我正确登录,图片就有效 - 所以如果有人想要一个如何实现这一目标的实例,请使用以上内容:)