0

We are trying to fetch the attachment inputstream of an inline image in HTML field of VSTS.

The code is in C#. We are using Visual Studion 2015 community edition and Microsoft Team Foundation Server Object Model 2012 and .NET framework 4.0 or higher.

Attachment URI variable value for below code : https://opshubautomation.visualstudio.com/WorkItemTracking/v1.0/AttachFileHandler.ashx?FileNameGuid=20157b52-3c9a-4bb7-bc63-f6b38fc1d54c&FileName=Att.png

The snippet of the code is mentioned below.

{
WebClient webClient = new WebClient();
webClient.Credentials = (ICredentials)connectionInfo.tfsServerConfig.Credentials;

    // 1. Approach 1 using webClient.DownloadFile() which downloads the file in current directory locally
    webClient.DownloadFile(attachmentURI, "aoaob.png");

    // 2. Approach 2 using webClient.DownloadData() which loads the byte array of the attachment hosted at given // attachment URI
    byte[] data = webClient.DownloadData(attachmentURI);
    return data;
}

Using the above code, we have tried accessing the same inline image (in any HTML field of an entity) hosted in TFS versions 2013, 2015, and 2017. For each versions of TFS, we are able to load a correct inline image attachment input stream. But on the contrary, using the same code and other infrastructures, we have tried loading the attachment input stream in Microsoft VSTS (same entity and same HTML field), and every time we get a corrupted PNG file of approximately 14 KB size irrespective of the size of the image being fetched by using the above attachment URI. The original size of the PNG image hosted at above attachment URI is 113 KB.

But, still we are not able to get a correct input stream in entity in Microsoft VSTS using the above method (while correct image loaded in TFS all versions mentioned above).

Please help us to resolve this issue or tell us anything that we are doing wrong.

Jay Patel
  • 1
  • 1
  • What's the result if you get another entity and HTML field in VSTS?Did it work as expectantly? – PatrickLu-MSFT Nov 28 '17 at 14:00
  • Even if we use different entity and different HTML field in VSTS, its the same. Its working fine with any entity any HTML field of TFS, but its not working with VSTS. – Jay Patel Nov 29 '17 at 06:41
  • Hi Jay, have you tried to use the [Rest API](https://www.visualstudio.com/en-us/docs/integrate/api/wit/attachments#download-an-attachment) more suitable for a web to download an attachment in VSTS, please refer the C# [code sample](https://github.com/Microsoft/vsts-dotnet-samples/blob/master/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/AttachmentsSample.cs#L69) – PatrickLu-MSFT Nov 30 '17 at 02:01
  • Hey, thanks for the suggestion of using REST, but I needed it by using Microsoft TFS Object Model (SOAP and not REST). If you can please suggest a way for the same, it will be a great help to us. – Jay Patel Nov 30 '17 at 06:26

1 Answers1

0

This could be caused by the authentication with VSTS. It is different with TFS. To authenticate to VSTS with WebClient, you need to enable alternate credential or create a Personal Access Token and then use basic auth, following is the code sample for your reference:

    WebClient wc = new WebClient();
    string altusername = "xxxxxx";
    string altpassword = "xxxxxx";
    string auth = altusername + ":" + altpassword;
    auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
    wc.Headers["Authorization"] = "Basic" + auth;
    Uri uri = new Uri("https://xxxxxxxx");
    wc.DownloadFile(uri, "aoaob.png");
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60