I can't seem to figure this out. When I go download the files manually (with the same URL that the WebClient is using), they seem to work just fine.
For background, I'm trying to download attachments on work items on our TFS2015 server and upload them to work items to Azure DevOps. I've tried both downloading the files to my local (which is how I know they're corrupted when downloaded) and also just directly getting the files as a Stream and using the WorkItemTrackingHttpClient.CreateAttachmentAsync() function to upload to Azure DevOps.
Anyone know what I'm doing wrong?
I've used the WebClient with both hard-coded credentials and my own (default) credentials. Both are exhibiting the same behavior.
public async Task<WorkItem> AddAttachmentFromSourceURL(int workItemID, string url, string fileName)
{
AttachmentReference attachment;
using (var webClient = WorkItemClientHelpers.CreateWebClient())
{
//this is only commented out so I could download the files (i.e. these are the two different methods I've tried).
//await using var attStream = webClient.OpenRead(url);
//attachment = WorkItemTrackingHttpClient.CreateAttachmentAsync(uploadStream: attStream, fileName: fileName).Result; // upload the file
var filePath = $"C:\\TempAttachment\\{fileName}";
webClient.DownloadFile(url, filePath);
await using FileStream attStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
attachment = WorkItemTrackingHttpClient.CreateAttachmentAsync(uploadStream: attStream, fileName: fileName).Result; // upload the file
}
JsonPatchDocument jsonPatchDoc = new JsonPatchDocument();
jsonPatchDoc.Add(new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/relations/-",
Value = new
{
rel = "AttachedFile",
url = attachment.Url,
attributes = new { comment = "Comments for the file " + fileName }
}
});
WorkItem updatedWorkItem = await WorkItemTrackingHttpClient.UpdateWorkItemAsync(jsonPatchDoc, workItemID);
return updatedWorkItem;
}
I found the answer in this post: WebClient download file corrupted (I swear, I did an exhaustive search before!)
Basically the file needs to be decompressed. Adding this class worked (this is the answer in the linked question):