简体   繁体   English

如何使用angular js从webapi url文件夹中检索图像?

[英]how to retrieve image from webapi url folder using angular js?

i am a newbie of angular js. 我是angular js的新手。 I am working on an existing project that using angular js for my company. 我正在为我的公司使用angular js的现有项目。 I would like to know how to retrieve an image that is inside my webapi project folder using angular js. 我想知道如何使用angular js检索webapi项目文件夹中的图像。 The webapi is declared as post method and i need to get the image of the user on my html view. 该webapi被声明为post方法,我需要在我的html视图上获取用户的图像。 I have created the webapi logic but when comes to angular js i am stucked and have no idea on how to do it. 我已经创建了webapi逻辑,但是当谈到Angular js时,我被卡住了,不知道如何去做。

Below is my web api post method 以下是我的Web API发布方法

 [AcceptVerbs("POST")]
    [Route("profile/{userid}/")]
    public byte[] GetScreenshot(string userid)
    {
        var imgPath = string.Format("{0}/{1}.png", Common.AppHelper.FolderPathProfilePicture, userid);

        if (!File.Exists(imgPath))
        {
                imgPath = string.Format("{0}/no.png", Common.AppHelper.FolderPathDeviceScreenshot);
            if (!File.Exists(imgPath))
                imgPath = System.Web.Hosting.HostingEnvironment.MapPath("~/doc/thumbnail") + "\no.png";
            if (!File.Exists(imgPath))
                return null;
        }
        return File.ReadAllBytes(imgPath);
    }

this is my html page 这是我的html页面

 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

    <span class="glyphicon glyphicon-pencil" style="margin-top: 7px;"></span>
    <div class="pull-right" style="margin-top: 5px;">
        <span class="text-info" data-ng-bind-html="accountLoginName | to_trusted" style="font-size: 12px;"></span>


 <!-- image must show at this img src -->

                <img src="" alt="" class="img-responsive img-circle" style="height: 25px; width: 25px; border-radius: 50% !important;" />&nbsp;<span class="caret"></span>
                <ul class="dropdown-menu">


                    <li><a ng-click="popupProfilePictureControl.showPopup(loginAccount)">Change profile picture</a></li>
                    <li><a href="#">Change corporation</a></li>
                    <li><a href="#">Change password</a></li>
                    <li class="divider"></li>
                    <li><a href="#">Help</a></li>
                    <li><a href="#">Support</a></li>

                    <li class="divider"></li>
                    <li><a href="#logout">Logout</a></li>
                </ul>




    </div>
</div>

I have no idea how to use the angular js to retrieve the image from my webapi. 我不知道如何使用angular js从我的webapi中检索图像。 Kindly guide me. 请指导我。

You can maybe use Base64 string of image, just replace the return value by string : 您可以使用图像的Base64字符串,只需将返回值替换为string即可:

return Convert.ToBase64String(File.ReadAllBytes(imgPath));

in JS after web api result (this using jquery) : 在Web API结果之后的JS中(使用jquery):

$(".img-responsive img-circle").attr("src",dataBase64Str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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