上传附件
更新时间: 2024-11-22 浏览次数: {{ hits }}

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

请求方式: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);

            var 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,

      "ErrorMessage": null,

      "Logined": true,

      "ReturnData": {

          "AttachmentId":   "f2418820-e818-4e92-9109-3fcab3a0211b"

      }

}

 

 

参数

说明

Successful

返回结果是否成功true/false

ErrorMessage

错误信息

Logined

未使用,忽略

ReturnData

返回的数据; AttachmentId为附件ID

DataType

返回的数据类型,默认0