无法在 Azure Devops 中打开附加到工作项的 Excel 文件 [英] Unable to open the Excel file attached to work item in Azure Devops

查看:63
本文介绍了无法在 Azure Devops 中打开附加到工作项的 Excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个工具,用于使用 Azure Devops REST API 将 excel 文件附加到工作项.我能够将 excel 文件添加到工作项,但是当我尝试打开它时,它只是说文件格式或文件扩展名无效".我感谢任何建议或任何投入.

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.

根据 Lance-Li 的建议,我将 excel 文件转换为字节并将其发送到请求正文中,如下所示

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");

现在使用 content-type=application\octet-stream 将上述工作项值发送到 post 方法.但我仍然面临格式问题.我有没有弄错?

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?

推荐答案

通常我们称之为 Attachments-Create(将附件上传到附件存储)然后添加附件(将附件存储中的附件添加到工作项)以将本地附件添加到工作项.

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.

问题原因:

使用first rest api(Attachments - Create)上传附件时要小心.在发布请求中,filename=xxx 没有收到文件的任何路径.它不会从桌面或其他文件夹中获取文件.

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.

经过多次测试,我重现了与您相同的问题.我还发现,如果我们运行 post 请求,它实际上只是创建一个同名文件,而不是获取本地文件.例如,如果我有一个包含内容 just for test 的本地 test.txt 文件,在我的请求正文中我有 要上传的用户文本内容",结果是得到一个名为test.txt的附件,内容为要上传的用户文本内容".

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".

所以我认为您可以上传带有文本内容的 xx.xlsx 文件.这就是我们收到 file foramt not valid 错误的原因.

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

两种解决方法:

1.上传为 二进制文件,将excel文件转换为二进制文件并将二进制内容粘贴到请求正文中.(关于是否可以转换excel以及如何转换,这是另一个问题,所以我不在这里讨论)

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.我最终找到的另一种解决方法是在 Invoke-RestMethod 命令中使用 -Infile 并且效果很好!

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

(如果你使用类似 postman 的东西来运行 api,contenttype=>application/octet-stream,body=>binary)

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

我关于Attachments-Create的Powershell脚本:

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

希望以上都能帮到你 :) 而且由于最初问题的原因只是关于第一个api的使用,所以我不会在这里谈论第二个......

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...

另外:如果有人使用 C# 客户端 api 上传二进制文件,请尝试使用 contenttype application\json, info from dhulipala murali.如果有人使用 PS/postman,application/octet-stream 在我身边工作得很好.

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.

这篇关于无法在 Azure Devops 中打开附加到工作项的 Excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆