跳转至

下载附件

功能描述

下载附件到本地。

请求方式

POST(HTTPS)

请求地址

https://www.h3yun.com/Api/DownloadBizObjectFile

参数说明

参数 参数类型 必须 说明
attachmentId String 附件 id

请求示例(C#)

DownloadFile("fa5a96d4-559c-46c0-9dcc-dcb6e427a94c"); // 调用下载附件方法,传递参数:附件 id

HttpClient client = new HttpClient();
// form-data: attachmentId、EngineCode
Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("attachmentId", fileId);
// 身份认证参数
FormUrlEncodedContent content = new FormUrlEncodedContent(keyValues);
content.Headers.Add("EngineCode", EngineCode);
content.Headers.Add("EngineSecret", EngineSecret);
// 请求执行
HttpResponseMessage result = client.PostAsync("https://www.h3yun.com/Api/DownloadBizObjectFile", content).Result;
using (Stream responseStream = result.Content.ReadAsStreamAsync().Result)
{
    // 创建本地文件
    string filePath = $"D:\\log\\{Guid.NewGuid().ToString()}.png";
    using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
    {
        byte[] bArr = new byte[1024];
        int size = responseStream.Read(bArr, 0, bArr.Length);
        while (size > 0)
        {
            stream.Write(bArr, 0, size);
            size = responseStream.Read(bArr, 0, bArr.Length);
        }
    }  
}

⚠️ 注意
如果表单中有上传附件操作,或者列表自定义代码有上传附件操作,并且该接口在表单提交的 OnSubmit 或者列表中的 OnSubmit 事件中被调用,用于下载提交动作中上传的附件,需注意:在 OnSubmit 事务未提交时,该 OpenAPI 接口读取的附件信息为临时信息。由于 OnSubmit 事件中处理附件时会删除临时文件,此时该 OpenAPI 接口无法下载附件。因此,必须等待表单或列表提交完成后,才能下载附件。

如果存在必须在提交过程中下载刚上传附件的场景,可在表单前端代码中编写下载附件逻辑。例如:

// 提交后事件
AfterSubmit: function(action, responseValue) {
    // 调用后端代码处理下载附件逻辑
}