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


C# StreamContent.ReadAsMultipartAsync方法代码示例

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


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

示例1: MergeImpl

        async Task<MergeResult> MergeImpl(IOwinContext context)
        {
            var content = new StreamContent(context.Request.Body);
            content.Headers.ContentType = MediaTypeHeaderValue.Parse(context.Request.ContentType);

            var result = new MergeResult();
            result.Graph = new Graph();

            var provider = await content.ReadAsMultipartAsync();
            foreach (var httpContent in provider.Contents)
            {
                var fileName = httpContent.Headers.ContentDisposition.FileName;
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    continue;
                }

                using (Stream fileContent = await httpContent.ReadAsStreamAsync())
                {
                    using (TextReader reader = new StreamReader(fileContent))
                    {
                        string data = await reader.ReadToEndAsync();

                        JToken jsonLd = JToken.Parse(data);

                        if (result.JsonLdContext == null)
                        {
                            result.JsonLdContext = (JObject)jsonLd["@context"];
                            result.JsonLdFrame = (string)jsonLd["@type"];
                        }

                        IGraph graph = Common.GraphFromJson(jsonLd);

                        result.Graph.Merge(graph, false);
                    }
                }
            }

            return result;
        }
开发者ID:johnataylor,项目名称:LinkedDataRevisited,代码行数:40,代码来源:Startup.cs

示例2: ReadAsMultipartAsync_ThrowsOnReadError

 public void ReadAsMultipartAsync_ThrowsOnReadError()
 {
     HttpContent content = new StreamContent(new ReadErrorStream());
     string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary);
     content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
     IOException exception = Assert.Throws<IOException>(() => content.ReadAsMultipartAsync().Result);
     Assert.NotNull(exception.InnerException);
     Assert.Equal(ExceptionAsyncStreamMessage, exception.InnerException.Message);
 }
开发者ID:RhysC,项目名称:aspnetwebstack,代码行数:9,代码来源:HttpContentMultipartExtensionsTests.cs

示例3: ReadAsMultipartAsync_PrematureEndOfStream_Throws

        public void ReadAsMultipartAsync_PrematureEndOfStream_Throws()
        {
            HttpContent content = new StreamContent(Stream.Null);
            var contentType = new MediaTypeHeaderValue("multipart/form-data");
            contentType.Parameters.Add(new NameValueHeaderValue("boundary", "\"{--\""));
            content.Headers.ContentType = contentType;

            Assert.Throws<IOException>(
                () => content.ReadAsMultipartAsync().Result,
                "Unexpected end of MIME multipart stream. MIME multipart message is not complete."
            );
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:12,代码来源:HttpContentMultipartExtensionsTests.cs

示例4: ReadAsMultipartAsync_ThrowsOnPrematureEndOfStream

 public void ReadAsMultipartAsync_ThrowsOnPrematureEndOfStream()
 {
     HttpContent content = new StreamContent(Stream.Null);
     string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary);
     content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
     Assert.Throws<IOException>(() => content.ReadAsMultipartAsync().Result);
 }
开发者ID:RhysC,项目名称:aspnetwebstack,代码行数:7,代码来源:HttpContentMultipartExtensionsTests.cs

示例5: ReadAsMultipartAsync_ReadErrorOnStream_Throws

        public void ReadAsMultipartAsync_ReadErrorOnStream_Throws()
        {
            HttpContent content = new StreamContent(new ReadErrorStream());
            var contentType = new MediaTypeHeaderValue("multipart/form-data");
            contentType.Parameters.Add(new NameValueHeaderValue("boundary", "\"--\""));
            content.Headers.ContentType = contentType;

            var ioException = Assert.Throws<IOException>(
                () => content.ReadAsMultipartAsync().Result,
                "Error reading MIME multipart body part."
            );
            Assert.NotNull(ioException.InnerException);
            Assert.Equal(ExceptionAsyncStreamMessage, ioException.InnerException.Message);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:14,代码来源:HttpContentMultipartExtensionsTests.cs

示例6: Post

        public HttpResponseMessage Post()
        {
            Stream reqStream = Request.Content.ReadAsStreamAsync().Result;
            MemoryStream tempStream = new MemoryStream();
            reqStream.CopyTo(tempStream);

            tempStream.Seek(0, SeekOrigin.End);
            StreamWriter writer = new StreamWriter(tempStream);
            writer.WriteLine();
            writer.Flush();
            tempStream.Position = 0;

            StreamContent streamContent = new StreamContent(tempStream);
            foreach (var header in Request.Content.Headers)
            {
                streamContent.Headers.Add(header.Key, header.Value);
            }

            var result = new HttpResponseMessage(HttpStatusCode.OK);

            if (Request.Content.IsMimeMultipartContent())
            {
                var task = streamContent.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider());
                task.Wait();
                MultipartMemoryStreamProvider provider = task.Result;
                using (var _repo = new WebsiteFileRepository(UnitOfWork))
                {
                    foreach (HttpContent content in provider.Contents)
                    {
                        WebsiteFile newFile = new WebsiteFile();
                        newFile.Id = Guid.NewGuid();

                        Stream stream = content.ReadAsStreamAsync().Result;
                        string filePath = HostingEnvironment.MapPath("~/Images/");
                        string fileName = content.Headers.ContentDisposition.FileName.Replace("\"", "");
                        string fullPath = Path.Combine(filePath, fileName);

                        using (var fileStream = File.Create(fullPath))
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            stream.CopyTo(fileStream);
                        }

                        //Add To DB
                        newFile.Filename = fileName;
                        _repo.InsertOrUpdate(newFile);
                    }
                    UnitOfWork.Save();
                }

                return result;
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }
        }
开发者ID:floriansofianos,项目名称:EasyWebsite,代码行数:57,代码来源:WebsiteFileController.cs

示例7: awaitReadAsync

 public static async Task<MultipartStreamProvider> awaitReadAsync(StreamContent stream)
 {
     return await stream.ReadAsMultipartAsync();
 }
开发者ID:sgarver,项目名称:RadMVC,代码行数:4,代码来源:MultipartFormParser.cs


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