[英]Unable to open the Excel file attached to work item in Azure Devops
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".我正在创建一个工具,用于使用 Azure Devops REST API 将 excel 文件附加到工作项。我能够将 excel 文件添加到工作项,但是当我尝试打开它时,它只是说“文件格式或文件扩展名无效” . 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编辑:根据 Lance-Li 的建议,我将 excel 文件转换为字节并将其发送到请求正文中,如下所示
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.现在使用 content-type=application\\octet-stream 将上述工作项值发送到 post 方法。 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.通常我们调用Attachments-Create (将附件上传到附件存储)然后添加一个附件(将附件从附件存储添加到工作项)以将本地附件添加到工作项。
Cause of the issue:问题原因:
We should be careful when we upload the attachment using first rest api(Attachments - Create).当我们使用first rest api(Attachments - Create)上传附件时,我们应该小心。 In the post request, the
filename=xxx
doesn't receive any path to the file.在 post 请求中,
filename=xxx
没有接收到文件的任何路径。 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.
我还发现,如果我们运行 post 请求,它实际上只是创建了一个同名的新文件,而不是获取本地文件。 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"
.例如,如果我有一个包含
just for test
内容的本地test.txt
文件,在我的请求正文中我有"User text content to upload"
,结果是获得一个名为test.txt
附件,其内容为"User text content to upload"
。
So I think you may upload a xx.xlsx
file with text content.所以我认为你可以上传一个带有文本内容的
xx.xlsx
文件。 That's why we got file foramt not valid
error.这就是为什么我们收到
file foramt not valid
错误的原因。
Two workarounds:两种解决方法:
1.Upload it as binary file , convert the excel file to binary file and paste the binary content to the request body. 1.上传为二进制文件,将excel文件转换为二进制文件,并将二进制内容粘贴到请求正文中。 (About is it possible to convert excel and how to do that, that's another issue so I don't talk about it here)
(关于是否可以转换excel以及如何转换,这是另一个问题,所以我不在这里讨论它)
2.Another workaround I finally found is using -Infile
in Invoke-RestMethod
command and it works well! 2.我最终找到的另一种解决方法是在
Invoke-RestMethod
命令中使用-Infile
并且效果很好!
(If you're using something like postman
to run the api, contenttype=> application/octet-stream
, body=> binary
) (如果你正在使用类似
postman
运行API,则contentType => application/octet-stream
,体=> binary
)
My Powershell script about Attachments-Create
:我关于
Attachments-Create
Powershell 脚本:
$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...希望以上都能帮到你:)而且由于最初问题的原因只是关于第一个api的使用,所以我不会在这里谈论第二个......
In addition: If someone is using C# client api to upload the binary file, try using contenttype application\\json
, info from dhulipala murali .另外:如果有人使用 C# 客户端 api 上传二进制文件,请尝试使用 contenttype
application\\json
,来自dhulipala murali 的信息。 And if someone is using PS/postman, application/octet-stream
works well at my side.如果有人使用 PS/邮递员,
application/octet-stream
在我身边工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.