将数据字节追加到内存流中以下载到 csv 中
Append memory stream with data byte to download in csv
我试图在循环中附加内存流,然后将其作为 csv 文件传递给 HTTPResponseMessage,但是当我下载该文件时,没有显示任何数据。
var stream = new System.IO.MemoryStream();
for (int i = 1; i <= pageCount; i++)
{
search = GetSearch(searchResultParam);
csvContent = GetCsvForSearchResult(search);
byte[] data = System.Text.Encoding.UTF8.GetBytes(csvContent);
stream.Write(data, 0, data.Length);
}
string fileName = "Review" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ".csv";
var result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK)
{
Content = new System.Net.Http.StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
但是如果我直接写入内存流,数据正在下载。
var stream = new System.IO.MemoryStream();
for (int i = 1; i <= pageCount; i++)
{
search = GetSearch(searchResultParam);
csvContent = GetCsvForSearchResult(search);
byte[] data = System.Text.Encoding.UTF8.GetBytes(csvContent);
stream = new System.IO.MemoryStream(data);
}
string fileName = "Review" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ".csv";
var result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK)
{
Content = new System.Net.Http.StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
在让客户端使用它之前先寻找流的开头
var stream = new System.IO.MemoryStream();
for (int i = 1; i <= pageCount; i++)
{
search = GetSearch(searchResultParam);
csvContent = GetCsvForSearchResult(search);
byte[] data = System.Text.Encoding.UTF8.GetBytes(csvContent);
stream.Write(data, 0, data.Length);
}
// will make the reads start at the beginning
stream.Seek(0, SeekOrigin.Begin);
string fileName = "Review" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ".csv";
var result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK)
{
Content = new System.Net.Http.StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
我试图在循环中附加内存流,然后将其作为 csv 文件传递给 HTTPResponseMessage,但是当我下载该文件时,没有显示任何数据。
var stream = new System.IO.MemoryStream();
for (int i = 1; i <= pageCount; i++)
{
search = GetSearch(searchResultParam);
csvContent = GetCsvForSearchResult(search);
byte[] data = System.Text.Encoding.UTF8.GetBytes(csvContent);
stream.Write(data, 0, data.Length);
}
string fileName = "Review" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ".csv";
var result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK)
{
Content = new System.Net.Http.StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
但是如果我直接写入内存流,数据正在下载。
var stream = new System.IO.MemoryStream();
for (int i = 1; i <= pageCount; i++)
{
search = GetSearch(searchResultParam);
csvContent = GetCsvForSearchResult(search);
byte[] data = System.Text.Encoding.UTF8.GetBytes(csvContent);
stream = new System.IO.MemoryStream(data);
}
string fileName = "Review" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ".csv";
var result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK)
{
Content = new System.Net.Http.StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
在让客户端使用它之前先寻找流的开头
var stream = new System.IO.MemoryStream();
for (int i = 1; i <= pageCount; i++)
{
search = GetSearch(searchResultParam);
csvContent = GetCsvForSearchResult(search);
byte[] data = System.Text.Encoding.UTF8.GetBytes(csvContent);
stream.Write(data, 0, data.Length);
}
// will make the reads start at the beginning
stream.Seek(0, SeekOrigin.Begin);
string fileName = "Review" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ".csv";
var result = new System.Net.Http.HttpResponseMessage(HttpStatusCode.OK)
{
Content = new System.Net.Http.StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;