我有一个ASP.NET Action,它返回一个MultiPart响应。当零件很小时,它可以正常工作,但是当零件内容的大小增加时,响应将在随机点处被截断。我怀疑这与响应中的流副本有关,但不确定如何克服这一点。有人可以帮忙吗?
以下是一些上下文:
这是主要Action的示例,显示了响应的构成方式以及如何将其写入响应
public ActionResult Index()
{
// Create content object to hold all ready parts. This is the actual response object
var content = new System.Net.Http.MultipartContent("byteranges", "multipart yo");
// Get all the parts to be sent
var partsArray = new MultipartContent[] {
GetHtmlPart(),
GetCssPart(),
GetJsPart()
};
// Add each part to the response
foreach (var item in partsArray)
{
if (item.Stream != null)
{
// Get part byte stream to add to the response
var partContent = new StreamContent(item.Stream);
// Set mime type
if (item.ContentType != null)
{
partContent.Headers.ContentType = new MediaTypeHeaderValue(item.ContentType);
}
// Set name information
if (item.FileName != null)
{
var contentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = item.FileName
};
partContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = contentDisposition.FileName,
FileNameStar = contentDisposition.FileNameStar
};
}
content.Add(partContent);
}
}
// send multipart contents with all part data to the response stream which is sent to the client
content.CopyToAsync(Response.OutputStream);
// set the HTTP content type header for response
Response.ContentType = content.Headers.ContentType.ToString();
// add allowance for CORS
Response.Headers.Add("Access-Control-Allow-Origin", "*");
// return setting the HTTP status code. In this case, 200 == OK
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
每个部分都以类似的方式添加,这是一个示例...
private MultipartContent GetHtmlPart()
{
// Get content to be sent
var content = GetHtmlContent();
// Convert to bytes since that is what we will send to client as
var bytes = Encoding.ASCII.GetBytes(content);
// Package as multi-part segment, and describe the mime details
var part = new MultipartContent()
{
ContentType = "text/plain",
FileName = "File.html",
Stream = new MemoryStream(bytes)
};
return part;
}