本文介绍了JIRA API附件名称包含发布的文件的路径全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直与JIRA API,并已经看到了我的要求不一致的结果。有时工作,有时没有。上周,我能上传附件的问题就好了,但现在一个老问题出现了:附件的名称包含发布文件的完整路径,因此附件无法打开。我使用JSON重新presentation发布文件:

I have been working with Jira API and have seen inconsistent results for my requests. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:

$array = array("file"=>"@filename");
json_encode($array);
...

这得到张贴的文件,但是当它张贴在JIRA中的文件名都是这样的问题是:

This gets the file posted but the problem is when it's posted the file names in JIRA are like this:

/var/www/user/text.text

不用说,它不能在JIRA打开。我以前有这个问题,那么它突然消失,现在它再次发生。我真的不明白这一点。顺便说一句,我不使用卷曲,即使它可能会在文档中推荐了这一请求。

Needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.

推荐答案

我意识到这个问题是有点老,但我有一个类似的问题。这似乎吉拉不一定修剪的文件名如预期。我可以用下面的修复。如果你使用PHP> = 5.5.0:

I realize this question is somewhat old but I had a similar problem. It seems Jira doesn't necessarily trim the filename as expected. I was able to fix it with the following. If you're using PHP >= 5.5.0:

$url = "http://example.com/jira/rest/api/2/issue/123456/attachments";
$headers = array("X-Atlassian-Token: nocheck");

$attachmentPath = "/full/path/to/file";
$filename = array_pop(explode('/', $attachmentPath));

$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);

$data = array('file'=>$cfile);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

$ch_error = curl_error($ch);

if ($ch_error){
    echo "cURL Error: $ch_error"; exit();
} else {
    print_r($result);
}

有关PHP< 5.5.0,但> 5.2.10(见这个bug

For PHP <5.5.0 but > 5.2.10 (see this bug):

$data = array('file'=>"@{$attachmentPath};filename={$filename}");

这篇关于JIRA API附件名称包含发布的文件的路径全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!