当前位置: 首页>>代码示例>>C#>>正文


C# MultipartFormDataContent.ReadAsByteArrayAsync方法代码示例

本文整理汇总了C#中System.Net.Http.MultipartFormDataContent.ReadAsByteArrayAsync方法的典型用法代码示例。如果您正苦于以下问题:C# MultipartFormDataContent.ReadAsByteArrayAsync方法的具体用法?C# MultipartFormDataContent.ReadAsByteArrayAsync怎么用?C# MultipartFormDataContent.ReadAsByteArrayAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.MultipartFormDataContent的用法示例。


在下文中一共展示了MultipartFormDataContent.ReadAsByteArrayAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Upload

        /// <summary>
        /// Synchronously uploads the file to Uploadcare.
        /// 
        /// The calling thread will be busy until the upload is finished.
        /// </summary>
        /// <returns> An Uploadcare file </returns>
        /// <exception cref="UploadFailureException"> </exception>
        public UploadcareFile Upload(bool? store = null)
        {
            var url = Urls.UploadBase();

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";

            try
            {
                var boundary = "Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
                using (var content = new MultipartFormDataContent(boundary))
                {
                    content.Add(new StringContent(_client.PublicKey), "UPLOADCARE_PUB_KEY");
                    if (store != null)
                        if (store.Value)
                            content.Add(new StringContent("1"), "UPLOADCARE_STORE");
                    else
                        content.Add(new StringContent("0"), "UPLOADCARE_STORE");
                    else
                        content.Add(new StringContent("auto"), "UPLOADCARE_STORE");
                    if (_file != null)
                        content.Add(new StreamContent(File.OpenRead(_file.FullName)), "file", _file.Name);
                    else
                        content.Add(new ByteArrayContent(_bytes), "file", _fileName);

                    var buffer = content.ReadAsByteArrayAsync().Result;
                    using (var reqStream = request.GetRequestStream())
                    {
                        reqStream.Write(buffer, 0, buffer.Length);
                    }

                    request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);

                    var fileId = _client.GetRequestHelper().ExecuteQuery(request, false, new UploadBaseData()).File;

                    return _client.GetFile(fileId);
                }
            }
            catch (Exception)
            {
                throw new UploadFailureException();
            }
        }
开发者ID:okolobaxa,项目名称:uploadcare-csharp,代码行数:50,代码来源:FileUploader.cs


注:本文中的System.Net.Http.MultipartFormDataContent.ReadAsByteArrayAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。