无法在Azure Devops中打开附加到工作项的Excel文件 [英] Unable to open the Excel file attached to work item in Azure Devops
问题描述
我正在创建一个用于使用Azure Devops REST API将excel文件附加到工作项的工具.我能够将excel文件添加到工作项,但是当我尝试打开它时,它只是显示文件foramt或文件扩展名"无效".我感谢您的任何建议或投入.
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?
推荐答案
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.
问题原因:
当我们使用第一个rest api(附件-创建)上传附件时,请务必小心.在发布请求中, 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.
经过几次测试,我重现了与您相同的问题.我还发现,如果我们运行发布请求,它实际上只是创建了一个同名的新文件,而不是获取本地文件.例如,如果我有一个本地 test.txt
文件,其内容为 just for test
,则在我的请求正文中,我有要上传的用户文字内容"
,结果是得到一个名为 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
文件.这就是为什么我们得到文件格式无效
错误.
So I think you may upload a xx.xlsx
file with text content. That's why we got file foramt not valid
error.
两种解决方法:
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的使用有关,因此在这里我不会谈论第二个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上传二进制文件,请尝试使用 dhulipala murali 中的内容类型 application \ json
>.如果有人使用PS/邮递员,那么 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屋!