我正在使用python将大量数据导入Azure Devops。我把所有的零件都做好了。我可以上传图片,我的问题是当我从Devops网站下载图片时,它不是一个有效的文件。我将分享我的图片上传代码
def convertAttachmentToJsonBinary(attachmentPath):
file = open(attachmentPath, 'rb')
file_data = file.read()
encoded_data = base64.b64encode(file_data)
decoded_data = encoded_data.decode()
data = "[" + decoded_data + "]"
return data
def patchWorkItemWithAttachment(case_id, att_id, att_url):
json_template = open("attachment.json", "r")
json_data = json.load(json_template)
json_data[0]['value']['url'] = att_url
json_data[0]['value']['attributes']['comment'] = "Adding attachment"
response = requests.patch(patch_workitem_url.replace("{id}", case_id), json=json_data, headers=headers)
print(response.json())
上传新案例时,我创建案例,添加附件,然后用新附件修补新创建的案例。同样,所有这些工作。我可以在线查看我的案例中的附件,但是当我下载它时,该文件无效。
我尝试将数据读取为字节数组,如下所示:
def convertAttachmentToJsonBinary(attachmentPath):
file = open(attachmentPath, 'rb')
encoded_data = bytearray(file.read())
data = "[" + str(encoded_data) + "]"
return data
我得到了同样的错误,上传的图像工程,但下载的图像是无效的
此错误与代码无关,但您正在下载的图像必须已损坏,这可能是paint无法打开此图像的原因
您可以尝试以下提到的方法:
1.尝试在不同的PC上打开图像
2.在另一个照片查看器应用程序中打开它
3.运行Windows疑难解答
4.尝试在不同的PC上打开图像
我再次测试,发现无法成功打开上传到附件Rest API返回的url。因此,上传时图像已损坏
请尝试使用
bytearray(image_file.read())
将图像转换为字节数组请替换
<>
的值相关问题 更多 >
编程相关推荐