Python问题,上载文件的名称和缩略图

2024-09-26 04:54:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python请求模块附加带有atlassian jira api的文件。附件成功,但附件名称变为file,附件缩略图不正确。下面是成功创建票证后jira的附件截图

enter image description here

下面给出了Python代码"Content-type" : "multipart/form-data"直接从前端javascript代码传递。请注意,atlassian文档提到,包含附件的多部分/表单数据参数的名称必须为file

file = request.files['file']
files = {'file': file.read()}
response = requests.post(JIRA_URL + '/' + path,
                         headers={'X-Atlassian-Token': 'no-check'},
                         auth=(JIRA_USER, JIRA_PASS),
                         files=files,
                         proxies={'https': PROXY})

1条回答
网友
1楼 · 发布于 2024-09-26 04:54:12

我使用jirapython,然后编写如下代码

        with open(localname, 'rb') as fd:
            jira.add_attachment(target_issue, attachment=fd, filename=att_filename)

其中localname是本地文件名,att_filename是您希望在问题中看到的实际文件名

jira python代码中有

    url = self._get_url('issue/' + str(issue) + '/attachments')

    fname = filename
    if not fname:
        fname = os.path.basename(attachment.name)

    if 'MultipartEncoder' not in globals():
        method = 'old'
        r = self._session.post(
            url,
            files={
                'file': (fname, attachment, 'application/octet-stream')},
            headers=CaseInsensitiveDict({'content-type': None, 'X-Atlassian-Token': 'nocheck'}))
    else:
        method = 'MultipartEncoder'

        def file_stream():
            return MultipartEncoder(
                fields={
                    'file': (fname, attachment, 'application/octet-stream')})
        m = file_stream()
        r = self._session.post(
            url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)

这有点棘手。我真的建议使用jirapython,而不是直接使用请求

相关问题 更多 >