Shuxia Ning
2025-01-17 a0bce3b366451b3ca94e676eb98dd7b415375c14
Component/Yw.BIMFace.SDK.Core/05-client/04-upload/BimfaceClient_Upload.cs
@@ -66,79 +66,86 @@
                fileName = fileInfo.Name;
            }
            var policyResult = await GetUploadFilePolicyAsync(fileName);
            string boundary = "----" + DateTime.Now.Ticks.ToString("x");// 边界符
            byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");     // 边界符开始。【☆】右侧必须要有 \r\n 。
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); // 边界符结束。【☆】两侧必须要有 --\r\n 。
            var textFormTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n" + "{1}\r\n";//文本模板
            var fileFormTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n";//文件模板
            var dic = new Dictionary<string, string>();
            dic.Add("name", fileName);
            dic.Add("key", policyResult.ObjectKey);
            dic.Add("policy", policyResult.Policy);
            dic.Add("OSSAccessKeyId", policyResult.AccessId);
            dic.Add("success_action_status", "200");
            dic.Add("callback", policyResult.CallbackBody);
            dic.Add("signature", policyResult.Signature);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(policyResult.Host);
            request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
            request.Method = WebRequestMethods.Http.Post;
            request.AllowWriteStreamBuffering = false;
            //request.KeepAlive = true;
            request.Timeout = -1;
            long contentLength = 0;
            var textBytesList = dic.Select(x =>
            return await Task.Run(() =>
            {
                var textFormItem = string.Format(textFormTemplate, x.Key, x.Value);
                byte[] textFormItemBytes = Encoding.UTF8.GetBytes(textFormItem);
                string boundary = "----" + DateTime.Now.Ticks.ToString("x");// 边界符
                byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");     // 边界符开始。【☆】右侧必须要有 \r\n 。
                byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); // 边界符结束。【☆】两侧必须要有 --\r\n 。
                var textFormTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n" + "{1}\r\n";//文本模板
                var fileFormTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n";//文件模板
                var dic = new Dictionary<string, string>();
                dic.Add("name", fileName);
                dic.Add("key", policyResult.ObjectKey);
                dic.Add("policy", policyResult.Policy);
                dic.Add("OSSAccessKeyId", policyResult.AccessId);
                dic.Add("success_action_status", "200");
                dic.Add("callback", policyResult.CallbackBody);
                dic.Add("signature", policyResult.Signature);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(policyResult.Host);
                request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                request.Method = WebRequestMethods.Http.Post;
                request.AllowWriteStreamBuffering = false;
                //request.KeepAlive = true;
                request.Timeout = -1;
                long contentLength = 0;
                var textBytesList = dic.Select(x =>
                {
                    var textFormItem = string.Format(textFormTemplate, x.Key, x.Value);
                    byte[] textFormItemBytes = Encoding.UTF8.GetBytes(textFormItem);
                    contentLength += beginBoundaryBytes.Length;
                    contentLength += textFormItemBytes.Length;
                    return textFormItemBytes;
                }).ToList();
                var fileFormItem = string.Format(fileFormTemplate, "file", fileName);
                byte[] fileFormItemBytes = Encoding.UTF8.GetBytes(fileFormItem);
                contentLength += beginBoundaryBytes.Length;
                contentLength += textFormItemBytes.Length;
                return textFormItemBytes;
            }).ToList();
                contentLength += fileFormItemBytes.Length;
            var fileFormItem = string.Format(fileFormTemplate, "file", fileName);
            byte[] fileFormItemBytes = Encoding.UTF8.GetBytes(fileFormItem);
            contentLength += beginBoundaryBytes.Length;
            contentLength += fileFormItemBytes.Length;
            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            contentLength += fileStream.Length;
            contentLength += endBoundaryBytes.Length;
            request.ContentLength = contentLength;
            var requestStream = request.GetRequestStream();
            textBytesList.ForEach(x =>
            {
                var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                contentLength += fileStream.Length;
                contentLength += endBoundaryBytes.Length;
                request.ContentLength = contentLength;
                var requestStream = request.GetRequestStream();
                textBytesList.ForEach(x =>
                {
                    requestStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
                    requestStream.Write(x, 0, x.Length);
                });
                requestStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
                requestStream.Write(x, 0, x.Length);
                requestStream.Write(fileFormItemBytes, 0, fileFormItemBytes.Length);
                //每次上传4M
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    requestStream.Write(buffer, 0, bytesRead);
                }
                requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
                fileStream.Close();
                requestStream.Close();
                var response = request.GetResponse();
                string responsetext = string.Empty;
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    responsetext = reader.ReadToEnd();
                }
                var result = JsonHelper.Json2Object<UploadFileResponse>(responsetext);
                result.TryThrowException();
                return result.Data;
            });
            requestStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
            requestStream.Write(fileFormItemBytes, 0, fileFormItemBytes.Length);
            //每次上传4M
            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                requestStream.Write(buffer, 0, bytesRead);
            }
            requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            fileStream.Close();
            requestStream.Close();
            var response = request.GetResponse();
            string responsetext = string.Empty;
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                responsetext = reader.ReadToEnd();
            }
            var result = JsonHelper.Json2Object<UploadFileResponse>(responsetext);
            result.TryThrowException();
            return result.Data;
        }
        /// <summary>