示例#1
0
        /// <summary>Reads as multipart.</summary>
        /// <param name="body">The body.</param>
        /// <returns></returns>
        public async virtual Task <MultipartHttpRequest> ReadAsMultipart(Stream body)
        {
            if (body == null)
            {
                return(null);
            }

            var context  = this.requestContextResolver.GetContext();
            var boundary = context.Request.ContentType.Boundary;
            MultipartHttpRequest multipart = null;
            MultipartSection     section;
            MultipartReader      reader = new MultipartReader(boundary, body);

            while ((section = await reader.ReadNextSectionAsync(context.RequestAborted).ConfigureAwait(false)) != null)
            {
                if (multipart == null)
                {
                    multipart = new MultipartHttpRequest(boundary);
                }

                var multipartSection = new MultipartHttpRequestSection
                {
                    ContentType        = section.ContentType,
                    ContentDisposition = section.ContentDisposition,
                };

                context.RegisterForDispose(multipartSection);

                if (!multipartSection.IsFileSection())
                {
                    using (var valueReader = new StreamReader(section.Body, Encoding.Default, bufferSize: 1024, detectEncodingFromByteOrderMarks: true, leaveOpen: true))
                    {
                        multipartSection.Value = await valueReader.ReadToEndAsync().ConfigureAwait(false);
                    }
                }
                else
                {
                    multipartSection.TempFileName = $"{Path.GetTempPath()}{Path.GetRandomFileName()}";

                    using (var targetStream = File.Create(multipartSection.TempFileName))
                    {
                        await section.Body.CopyToAsync(targetStream, 1024, context.RequestAborted).ConfigureAwait(false);
                    }
                }

                multipart.Sections.Add(multipartSection);
            }

            return(multipart);
        }
示例#2
0
        /// <summary>Simples the patch.</summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public SimpleMultipartRs SimpleMultipart([InBody] MultipartHttpRequest model)
        {
            string fileData     = null;
            var    filesSection = model.Sections.FirstOrDefault(m => m.Name == "Files");

            if (filesSection != null)
            {
                var stream = model.Sections.FirstOrDefault(m => m.Name == "Files").GetStream();

                using (var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true))
                {
                    fileData = reader.ReadToEnd();
                }
            }

            return(new SimpleMultipartRs
            {
                Value = model.Sections.FirstOrDefault(m => m.Name == "Value")?.Value,
                OtherValue = model.Sections.FirstOrDefault(m => m.Name == "OtherValue")?.Value,
                TextFileData = fileData
            });
        }
示例#3
0
 /// <summary>Posts the specified data.</summary>
 /// <param name="data">The data.</param>
 /// <param name="simpleMultiPartParameter">The simple multi part parameter.</param>
 /// <param name="simpleMultiPartParameterNullableInt">The simple multi part parameter nullable int.</param>
 public void Post([InBody] MultipartHttpRequest data, string simpleMultiPartParameter, int?simpleMultiPartParameterNullableInt)
 {
 }