简体   繁体   English

无法在 Azure Devops 中打开附加到工作项的 Excel 文件

[英]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.

相关问题 当我在Azure Devops管道中运行构建(vstest)时无法打开Excel - Unable to open Excel when i run build (vstest) in azure devops pipeline 如何以编程方式链接 Azure DevOps 中的分支和工作项 - How to link a branch and work item in Azure DevOps programmatically 无法访问Azure DevOps工作项的“分配给”字段 - Not able to access the Assigned To field of a Azure DevOps Work Item Azure DevOps 使用 API 获取工作项通知 - Azure DevOps Get work item notification using API 使用 Asp.net 更新单个工作项 Azure Devops - Updates a single work item Azure Devops using Asp.net Azure DevOps API 创建工作项返回 404 错误 - Azure DevOps API creating work item returns 404 error 无法在 Azure DevOps 发布管道中提取 .NET 单文件应用程序 - Unable to extract .NET Single File Application in Azure DevOps release pipeline 无法将 excel 文件上传到 Azure 存储 Blob - unable to upload a excel file into Azure Storage Blob 将Excel转换为PDF后无法打开该文件 - After converting Excel to PDF Unable to open that file Azure DevOps Rest API - 如何在工作项中获取指定用户的显示名称 - Azure DevOps Rest API - How to grab assigned user's display name in work item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM