跳转至

上传附件

功能描述

通过本接口上传附件,附件大小不可超过 10 MB。

请求方式

POST(HTTPS)

请求地址

https://www.h3yun.com/OpenApi/UploadAttachment?SchemaCode=SchemaCode&FilePropertyName=FilePropertyName&BizObjectId=BizObjectId

参数说明

参数 参数类型 必须 说明
SchemaCode String 表单编码
ChildSchemaCode String 子表单编码
FilePropertyName String 表单控件编码(如 F0000002
BizObjectId String 表单 ObjectId

请求示例(C#)

// 身份认证参数
WebHeaderCollection head = new WebHeaderCollection();
head.Add("EngineCode", "");
head.Add("EngineSecret", "");
// 请求的 URL
string url = "https://www.h3yun.com/OpenApi/UploadAttachment?SchemaCode=D000024chuangjian&FilePropertyName=F0000011&BizObjectId=7140d02a-6d19-461b-b7dc-892f202b1566";
// 读取本地文件
FileStream file = new FileStream("读取的文件名", FileMode.Open);
long fd = file.Length;
byte[] bytes = new byte[fd];
file.Read(bytes, 0, Convert.ToInt32(fd));
file.Close();
// 调用上传方法
string filename = "新增.png"; // 氚云显示的文件名,必须带后缀(例:file.jpg)       
string contentType = "image/png"; // 文件 ContentType,可参考 https://www.w3school.com.cn/media/media_mimeref.asp(例:image/jpg)
string result = _UploadFile(url, bytes, filename, head, contentType);

/// <summary>
/// 上传方法
/// </summary>
/// <param name="url">上传接口 Url</param>
/// <param name="bytes">文件 Byte</param>
/// <param name="fileName">文件名</param>
/// <param name="headers">请求 Headers</param>
/// <param name="contentType">文件 ContentType</param>
/// <returns></returns>

private static string _UploadFile(string url, byte[] bytes, string fileName, WebHeaderCollection headers, string contentType)
{
    Uri oUri = new Uri(url);
    string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
    // The trailing boundary string
    byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");
    // The post message header
    StringBuilder sb = new StringBuilder();
    sb.Append("--");
    sb.Append(strBoundary);
    sb.Append("\r\n");
    sb.Append("Content-Disposition:   form-data; name=\"media\";");
    sb.Append(" filename=\"");
    sb.Append(fileName);
    sb.Append("\"");
    sb.Append("\r\n");
    sb.Append("Content-Type: ");
    sb.Append(contentType + "\r\n\r\n");
    string strPostHeader = sb.ToString();
    byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
    // The WebRequest
    HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
    oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
    oWebrequest.Method = "POST";
    if (headers != null)
    {
        foreach (string key in headers.Keys)
        {
            oWebrequest.Headers.Add(key, headers[key]);
        }
    }
    // This is important, otherwise   the whole file will be read to memory anyway...
    oWebrequest.AllowWriteStreamBuffering = false;
    long length = postHeaderBytes.Length + bytes.Length + boundaryBytes.Length;
    oWebrequest.ContentLength = length;
    using (Stream oRequestStream = oWebrequest.GetRequestStream())
    {
        // Write the post header
        oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
        oRequestStream.Write(bytes, 0, bytes.Length);
        // Add the trailing boundary
        oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
        WebResponse oWResponse = oWebrequest.GetResponse();
        using (Stream s = oWResponse.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(s))
            {
                string sReturnString = sr.ReadToEnd();
                return sReturnString;
            }
        }
    }
}

返回结果

参数 说明
Successful 返回结果是否成功 true/false
ErrorMessage 错误信息。
Logined 未使用,忽略。
ReturnData 返回的数据。AttachmentId 为附件 ID。
DataType 返回的数据类型,默认 0。

返回结果示例:

{
    "Successful": true,
    "ErrorMessage": null,
    "Logined": true,
    "ReturnData": {
        // 附件 ID
        "AttachmentId": "f2418820-e818-4e92-9109-3fcab3a0211b"
    }
}