示例#1
0
        private string Upload(NameValueCollection nvc, string fileName, string contentType, byte[] fileBytes)
        {
            var formGenerator = new PostFormGenerator();

            // 所有表单数据
            var bytesList = new ArrayList();

            // 普通表单
            foreach (var key in nvc.AllKeys)
            {
                bytesList.Add(formGenerator.CreateFieldData(key, nvc[key]));
            }

            // 上传表单
            bytesList.Add(formGenerator.CreateFieldData("ImageUpload", fileName, contentType, fileBytes));

            // 合成所有表单并生成二进制数组
            byte[] allBytes = formGenerator.JoinBytes(bytesList);

            // 返回的内容
            byte[] responseBytes;

            //上传服务地址


            // 上传到指定Url
            bool success = formGenerator.UploadData(_uploadServiceUrl, allBytes, out responseBytes);

            if (!success)
            {
                _logger.Error("Upload image failed! ");
                var message = "Params: ";
                foreach (var key in nvc.AllKeys)
                {
                    message += string.Format("[{0}={1}]  ", key, nvc[key]);
                }
                _logger.Error("------" + message);
            }
            return(Encoding.UTF8.GetString(responseBytes));
        }
示例#2
0
        private string Upload(NameValueCollection nvc, string fileName, string contentType, byte[] fileBytes)
        {
            var formGenerator = new PostFormGenerator();

            // 所有表单数据
            var bytesList = new ArrayList();

            // 普通表单
            foreach (var key in nvc.AllKeys)
            {
                bytesList.Add(formGenerator.CreateFieldData(key, nvc[key]));
            }

            // 上传表单
            bytesList.Add(formGenerator.CreateFieldData("ImageUpload", fileName, contentType, fileBytes));

            // 合成所有表单并生成二进制数组
            byte[] allBytes = formGenerator.JoinBytes(bytesList);

            // 返回的内容
            byte[] responseBytes;

            //上传服务地址

            // 上传到指定Url
            bool success = formGenerator.UploadData(_uploadServiceUrl, allBytes, out responseBytes);

            if (!success)
            {
                _logger.Error("Upload image failed! ");
                var message = "Params: ";
                foreach (var key in nvc.AllKeys)
                {
                    message += string.Format("[{0}={1}]  ", key, nvc[key]);
                }
                _logger.Error("------" + message);
            }
            return Encoding.UTF8.GetString(responseBytes);
        }