我有一个 ASP.NET Web API 操作,它创建一个文件并返回一个流内容:
public HttpResponseMessage Get(string filePath)
{
// create file
var file = FileConverter.GenerateExcelFile(filePath);
var stream = new FileStream(filePath, FileMode.Open);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(filePath);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
我想使用 HttpClient 下载返回的文件。这是我现在拥有的:
client.GetAsync(address).ContinueWith(
(requestTask) =>
{
HttpResponseMessage response = requestTask.Result;
// response.EnsureSuccessStatusCode();
response.Content.ReadAsStreamAsync().ContinueWith(
(readTask) =>
{
var stream = readTask.Result;
});
});
得到结果后如何强制实际下载?
编辑:我正在使用 ASP.NET 4.0 WebForms。