我正在从S3文件返回文件流。由于该流不可搜索,因此将其放入MemoryStream中以获取长度。该文件确实下载到浏览器。当我打开文件时,我看到了正确的csv数据,但在数据后也有页面HTML。不确定我缺少什么。
var streamResult = FileStream(bucket, fileName);
if (streamResult.Stream != null)
{
//using (StreamReader reader = new StreamReader(streamResult.Stream))
//using (Stream stream = streamResult.Stream)
using (var ms = new MemoryStream())
{
streamResult.Stream.CopyTo(ms);
ms.Position = 0;
//byte[] bytes = System.Text.Encoding.UTF8.GetBytes(ms);
HttpContext context = HttpContext.Current;
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + returnFileName);
context.Response.AddHeader("Content-Type", "application/vnd.ms-excel");
context.Response.AddHeader("Content-Length", ms.Length.ToString());
//context.Response.ContentType = "application/octet-stream";
//context.Response.ClearContent();
ms.WriteTo(context.Response.OutputStream);
//context.Response.OutputStream.Write(bytes, 0, bytes.Length);
}
}
答案 0 :(得分:0)
更新:只需在context.Response.Flush();
之后添加ms.WriteTo(context.Response.OutputStream);