简体   繁体   English

使用Azure DevOps小部件导出图像

[英]Export Images with an Azure DevOps Widget

I am building a widget for Azure DevOps to create custom dashboards and export functionalities. 我正在为Azure DevOps构建小部件,以创建自定义仪表板和导出功能。 Till now, everything works fine, till I export a multiline text field containing images to a word document. 到现在,一切正常,直到我将包含图像的多行文本字段导出到Word文档。 The images are containing urls to azure, who will not be displayed in word, because you are not authenticated there. 图片中包含蔚蓝的网址,由于您未通过身份验证,因此不会以文字形式显示。

The field contains just simple html data: 该字段仅包含简单的html数据:

<div>
    <img src="https://dev.azure.com/xxx/xxx-xxx-xxxx-xxx-xxxxxxxxxxx/_apis/wit/attachments/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx?fileName=image.png">
</div>

I Tried to use a HTML5 <canvas> with toDataURL() to convert images to base64, but this returned me an Cross-Origin error. 我试图使用HTML5 <canvas>toDataURL()将图像转换为base64,但这返回了我一个跨域错误。

Beside this option I also tried to do a http request to get the image as data, but this returns me unauthenticated messages. 除了此选项外,我还尝试执行http请求以将图像作为数据获取,但这会返回未经身份验证的消息。

It feels really weird that I can see the Image, but that I can't do anything with it. 我可以看到图像,但对此却无能为力,这真是太不可思议了。 Does someone have a solution for getting the images and converting it to base64? 有人有解决方案来获取图像并将其转换为base64吗? Or maybe there is some api to download the images? 或者,也许有一些API下载图像? The code for the widget is written in Javascript. 小部件的代码用Javascript编写。

I found a solution in the Azure DevOps REST Client documentation . 我在Azure DevOps REST客户端文档中找到了一个解决方案。

You can solve the problem by getting the attachmentId from the image url. 您可以通过从图片网址获取attachmentId来解决此问题。 Keep in mind that the url structure will be different in API version 4.1 and 5.0! 请记住,URL结构在API 4.1和5.0版本中会有所不同!

After you got the Id, you can get the Image as an ArrayBuffer and convert this to an base64 image. 取得ID后,您可以将图像作为ArrayBuffer ,并将其转换为base64图像。

var client = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
client.getAttachmentContent(attachmentId).then(function(imageData) {
    var base64 = btoa(new Uint8Array(imageData).reduce((data, byte) => data + String.fromCharCode(byte), ''));
    var iamge = "data:image/jpg;base64," + base64";
});

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

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