【问题标题】:Create Attachment in Azure Project is not working在 Azure 项目中创建附件不起作用
【发布时间】:2022-01-19 10:54:33
【问题描述】:

在 Azure 项目中创建附件不起作用。在开始时创建附件。但现在使用相同的代码和相同的图像在每次调用时创建黑色图像。 https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/attachments/create?view=azure-devops-rest-6.0#examples

代码示例

$data = fopen("test.png", 'rb');
$size = filesize($file);
$contents = fread($data, $size);
fclose($data);
$encodedData = base64_encode($contents);
        
$url = "https://dev.azure.com/OrganizationName/ProjectId/_apis/wit/attachments?fileName=test.png&uploadType=simple&api-version=6.0";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$header = array("Authorization:Basic ".base64_encode($params['username'].":".$params['token']),"Content-Type:application/octet-stream");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
if ($params['request']) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

回应是

stdClass Object
    (
    [id] => f1f55c9e-8f48-4408-90f0-69ebb3c4d0ff
    [url] => https://dev.azure.com/muhammadhaneef0757/749218ab-92a7-4ed1-86d8-738d8c3888e8/_apis/wit/attachments/f1f55c9e-8f48-4408-90f0-69ebb3c4d0ff?fileName=test.png
    )
when i check url in browser there is showing black image

【问题讨论】:

    标签: azure azure-devops attachment


    【解决方案1】:

    已解决的代码示例

    $data = fopen("test.png", 'rb');
    $size = filesize($file);
    $contents = fread($data, $size);
    fclose($data);
            
    $url = "https://dev.azure.com/{OrganizationName}/{ProjectId}/_apis/wit/attachments?fileName=test.png&uploadType=simple&api-version=6.0";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $header = array("Authorization:Basic ".base64_encode($params['username'].":".$params['token']),"Content-Type:application/octet-stream");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
    $response = json_decode($response);
    

    与工作项的链接

    $postRequest = [];
    $postRequest[] = ["op"=>"add",
    "path"=> ("/relations/-"),
    'from' => null,
    "value"=> ["rel"=>"AttachedFile",
                "url"=>$response->url
                
                ]
    ];
    $url = "https://dev.azure.com/{OrganizationName}/{ProjectId}/_apis/wit/workitems/{workitemId}?api-version=6.0";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $header = array("Authorization:Basic ".base64_encode($params['username'].":".$params['token']),"Content-Type:application/json-patch+json");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postRequest));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    

    【讨论】:

    • 您的答案可以通过添加有关代码的作用以及它如何帮助您的更多信息来改进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多