Hi i have followed Microsoft Doccument and uploaded Attachment devops and successfully attached it to work item. but when i dowloaded the file from devops userinteface file ish showing but not able to open it.
here is code
converting Iformfile object to binary file Thanks in advace:)
foreach (var item in enterddetals.Attachments)
{
TicketAttachemnts attachemnt = new TicketAttachemnts();
attachemnt.TicketNo = enterddetals.TicketNo;
attachemnt.Name = Path.GetFileName(item.FileName);
attachemnt.FileType = Path.GetExtension(item.FileName);
using (var target = new MemoryStream())
{
item.CopyTo(target);
attachemnt.FileContent = target.ToArray();
}
string attachjson = JsonConvert.SerializeObject(attachemnt);
response = await _azuredevopsmnger.CreateAttachement("DevOps/CreateAttachement",attachjson);
Creating attachment and linking it to work item
ResultClass res = new ResultClass();
var personalaccesstoken = "";
string baseUrl = "https://.../_apis/wit/attachments?fileName={0}&api-version=5.0";
string baseUrl2 = "https:/.../_apis/wit/workitems/{0}?api-version=5.0";
string url= string.Format(baseUrl, attachments.Name);
string url2= string.Format(baseUrl2, attachments.TicketNo);
string msg1 = "Ticket created successfully!! Your Ticket ID is :{0}";
string msg2 = "Ohh....Ticket creation Failed !! Reson: {0}";
var json = new StringContent(JsonConvert.SerializeObject(attachments.FileContent), Encoding.UTF8, "application/json-patch+json");
try
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", personalaccesstoken))));
using (HttpResponseMessage response = client.PostAsync(url,json).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
JObject data = JObject.Parse(responseBody);
string result = Convert.ToString(data["url"]);
List<Root> atc = new List<Root>();
Root att = new Root();
Attributes at = new Attributes();
Value v = new Value();
at.comment = "Spec for the work";
v.rel = "AttachedFile";
v.url = result;
v.attributes = at;
att.op = "add";
att.path = "/relations/-";
att.value = v;
atc.Add(att);
json = new StringContent(JsonConvert.SerializeObject(atc), Encoding.UTF8, "application/json-patch+json");
// return responseBody;
}
using (HttpResponseMessage response = client.PatchAsync(url2, json).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
JObject data = JObject.Parse(responseBody);
int result = Convert.ToInt32(data["id"]);
res.IsSuccess = true;
res.ticketID = result;
res.message = string.Format(msg1, result);
return res;
}
[file stored in devops ][1]
[Error Msg when downloaded and opened it][1]
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The problem comes from the json body you passed to the client.PostAsync method when you created a attachment.
You should read the files to bytes and build a ByteArrayContent instead of StringContent.
I donot know above item instance of yours. But when you create a attachment. Firstly, you need to read your files to bytes array. For example:
var bitArr = File.ReadAllBytes(@"C:TestUploadTemppic.png");
Then Convert to ByteArrayContent. Eg.
var json = new ByteArrayContent(bitArr); HttpResponseMessage response = client.PostAsync(url, json).Result;
Please see below example:
public string uploadAttachment() {
#read the files as byteArray
var bitArr = File.ReadAllBytes(@"C:TestUploadTemppic.png");
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", personalaccesstoken))));
var json = new ByteArrayContent(bitArr); ##convert to ByteArrayContent
json.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage response = client.PostAsync(url, json).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
JObject data = JObject.Parse(responseBody);
string result = Convert.ToString(data["url"]);
return result;
}
}
I used above code to created attachments. And the attachments can be successfully opened when downloaded from UI.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0