有趣的小功能【点击按钮下载附件】升级版

有趣的小功能【点击按钮下载附件】升级版

之前写了一篇此类的文章 https://bbs.mingdao.net/topic/1297,只是它仅支持单个附件下载。因为明道的界面推送 只能打开一个链接,那么就需要我们去想办法把附件整合在一起,那就打压缩包了。

整体方案流程如下:

左侧分支:原来的单个下载附件的模式。

右侧分支:

第一步代码块处理附件 多个附件 写入到 zip 中,zip 内容 base64 编码,调用 API 接口写入到工作流新的暂存附件字段中

import io

import zipfile

import base64

import requests

import urllib.parse

import json

#filename = urllib.parse.unquote(urllib.parse.parse_qs(urllib.parse.urlparse(obj["DownloadUrl"]).query)["attname"][0])

def download_attachments(attachment_urls):

attachments = []

for obj in attachment_urls:

response = requests.get(obj["DownloadUrl"])

attachment = {

"filename": obj["original_file_name"],

"content": response.content

}

attachments.append(attachment)

return attachments

def compress_to_zip(attachments):

zip_data = io.BytesIO()

with zipfile.ZipFile(zip_data, "w") as zipf:

for attachment in attachments:

zipf.writestr(attachment["filename"], attachment["content"])

zip_data.seek(0)

return zip_data.read()

def generate_base64(zip_content):

base64_data = base64.b64encode(zip_content).decode("utf-8")

return base64_data

inparam={

"appKey": input["appKey"],

"sign": input["sign"],

"worksheetId": input["worksheetId"],

"rowId": input["rowid"]

}

imgresponse=requests.post(input["url"]+'/v2/open/worksheet/getRowByIdPost',json=inparam)

imgresult=imgresponse.json()

attachment_urls =json.loads(imgresult["data"][input["oldFileid"]])

attachments = download_attachments(attachment_urls)

zip_content = compress_to_zip(attachments)

base64_data = generate_base64(zip_content)

param={

"appKey": input["appKey"],

"sign": input["sign"],

"worksheetId": input["worksheetId"],

"rowId": input["rowid"],

"controls": [

{

"controlId": input["newFileid"],

"value": "",

"editType": "0",

"valueType": "2",

"controlFiles": [

{

"baseFile": base64_data ,

"fileName": input["name"]+".zip"

}

]

}

]

}

response=requests.post(input["url"]+'/v2/open/worksheet/editRow',json=param)

result=response.json()

output={'result':result}

第二步 获取 zip 附件的链接 ,下载。

效果如下图:

感谢过去一年的陪伴与支持,我给大家提前拜个早年啦!祝各位在新一年里龙行好运,身体健康,生意兴隆!

💎 相关推荐

dnf史诗宝珠有哪些,DNF史诗宝珠属性整理
365体育官网 客户端

dnf史诗宝珠有哪些,DNF史诗宝珠属性整理

📅 08-18 👁️ 6741
弹的成语
365体育官网 客户端

弹的成语

📅 07-22 👁️ 2807
保护自己免受虚拟币诈骗的6种方法和对策
365体育官网 客户端

保护自己免受虚拟币诈骗的6种方法和对策

📅 10-01 👁️ 3527