Unable to open the Excel file attached to work item in Azure Devops

南楼画角 提交于 2021-02-05 09:30:12

问题


I am creating a tool for attaching the excel files to work items using Azure Devops REST API.I am able to add the excel file to work item but when I was trying to open that it just says "file foramt or file extension not valid". I appreciate any suggestions or any inputs.

Edited: As per the sugesstion of Lance-Li , i converted the excel file to bytes and send that in request body like below

 byte[] filedata = File.ReadAllBytes(filepath);
dynamic WorkItem = new List<dynamic>() {

                new
                {

                  filedata,
                }

            };
    var WorkItemValue = new StringContent(JsonConvert.SerializeObject(WorkItem),Encoding.UTF8, "application/json-patch+json");

now sending above workitem value to post method with content-type=application\octet-stream. but still i am facing format issue.did I make any mistake?


回答1:


Normally we call Attachments-Create(Upload the attachment to the attachment store) and then Add an attachment(Add the attachment from attachment store to the work item) to add local attachment to workitem.

Cause of the issue:

We should be careful when we upload the attachment using first rest api(Attachments - Create). In the post request, the filename=xxx doesn't receive any path to the file. It won't fetch the file from desktop or other folders.

After several tests, I reproduced the same issue like yours. Also I find that if we run the post request, it actually just created one new file with same name instead of fetching local file. For example, if I have a local test.txt file with content just for test, in my request body I have "User text content to upload", the result is to get one attachment named test.txt with content "User text content to upload".

So I think you may upload a xx.xlsx file with text content. That's why we got file foramt not valid error.

Two workarounds:

1.Upload it as binary file, convert the excel file to binary file and paste the binary content to the request body. (About is it possible to convert excel and how to do that, that's another issue so I don't talk about it here)

2.Another workaround I finally found is using -Infile in Invoke-RestMethod command and it works well!

(If you're using something like postman to run the api, contenttype=>application/octet-stream, body=>binary)

My Powershell script about Attachments-Create:

$token = "YourPat"

$url="https://dev.azure.com/YourOrgName/_apis/wit/attachments?fileName=YourFileName.xlsx&api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$filepath="YourFilePath\YourFileName.xlsx"

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -InFile $filepath -ContentType application/octet-stream

Write-Host $response

Hope all above helps :) And since the cause of the original issue is only about the usage of first api, so I won't talk about the second one here...

In addition: If someone is using C# client api to upload the binary file, try using contenttype application\json, info from dhulipala murali. And if someone is using PS/postman, application/octet-stream works well at my side.



来源:https://stackoverflow.com/questions/60692208/unable-to-open-the-excel-file-attached-to-work-item-in-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!