本文整理汇总了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;
}
示例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);
}
示例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."
);
}
示例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);
}
示例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);
}
示例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"));
}
}
示例7: awaitReadAsync
public static async Task<MultipartStreamProvider> awaitReadAsync(StreamContent stream)
{
return await stream.ReadAsMultipartAsync();
}