如何从TFS api流式传输zip文件

时间:2018-02-23 20:11:48

标签: c# tfs azure-devops

我正在使用TFS2018 api,我正在尝试检索解决方案的zip文件,但我总是收到内部服务器错误。

internal async Task<bool> GetSourceZipFile(string sourceVersionId)
    {
        using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(tfsUser, tfsPass) })
        using (var client = new HttpClient(handler))
        {
            try
            {
                client.BaseAddress = new Uri(tfsServer);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
                var tempFolder = "c:\\temp\\test";
                tempFolder = HttpUtility.UrlEncode(tempFolder);
                var url = $"DefaultCollection/_api/_versioncontrol/itemContentZipped?path={tempFolder}&version={sourceVersionId}";

                using (var file = await client.GetStreamAsync(url).ConfigureAwait(false))
                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);
                    var s = memoryStream.ToArray();
                    var f = s;
                };                 
            }
            catch (Exception ex)
            {
                //  LOGGING                    
            }

            return true;
        }
    }

我不确定zip文件是否由TFS服务器生成。我需要专门设置它吗?知道为什么这不起作用吗?

1 个答案:

答案 0 :(得分:1)

您将本地文件夹传递给REST API中的$/MyTeamProject/DEV/SomeCode参数。路径应该是源代码管理中的项目(例如:&s[i]),而不是本地文件系统。