尝试从 WebApi 下载文件时任务被取消
Task was cancelled when trying to download file from WebApi
当我尝试从 Web API 下载 zip 文件时,出现错误“任务被取消”。我做错了什么?
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Threading.Tasks.TaskCanceledException: A task was canceled.
using (var ms = new MemoryStream())
{
using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var attachment in item)
{
var entry = zipArchive.CreateEntry(attachment.ItemAnalisado.Arquivo.Nome, CompressionLevel.Fastest);
using (var stream = entry.Open())
{
var dadosArquivo = File.ReadAllBytes(
Path.Combine(CaminhoImagens,
attachment.ItemAnalisado.Arquivo.ProcessoId.ToString(),
attachment.ItemAnalisado.Arquivo.SubPastaId.ToString(),
attachment.ItemAnalisado.Arquivo.Id.ToString()));
stream.Write(dadosArquivo, 0, dadosArquivo.Length);
//stream.Position = 0;
stream.Close();
}
}
}
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
ms.Position = 0;
response.Content = new StreamContent(ms);
ms.Dispose();
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "teste.zip";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
return response;
}
我在尝试下载调用我的 API 的 Excel 文件时遇到此错误。我没有在这个方法上实现 ATP,所以一开始很困惑。这是我的初始代码:
public IHttpActionResult Get([FromUri] string id, [FromUri] SurveyReportRequestType requestType) {
byte[] excelData = System.Text.Encoding.UTF8.GetBytes("Boobs out!");
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
using (var stream = new MemoryStream(excelData))
{
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Data.xls"
};
}
return ResponseMessage(result);
}
经过思考并做出一些改变后,得出了以下结论:
...
byte[] excelData = System.Text.Encoding.UTF8.GetBytes("Boobs aout!");
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(excelData);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Data.xls"
};
return ResponseMessage(result);
...
如您所见,我将 stream 包含在 using 语句中。这导致请求未完成,因为内容不可用于响应。
当我尝试从 Web API 下载 zip 文件时,出现错误“任务被取消”。我做错了什么?
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Threading.Tasks.TaskCanceledException: A task was canceled.
using (var ms = new MemoryStream())
{
using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var attachment in item)
{
var entry = zipArchive.CreateEntry(attachment.ItemAnalisado.Arquivo.Nome, CompressionLevel.Fastest);
using (var stream = entry.Open())
{
var dadosArquivo = File.ReadAllBytes(
Path.Combine(CaminhoImagens,
attachment.ItemAnalisado.Arquivo.ProcessoId.ToString(),
attachment.ItemAnalisado.Arquivo.SubPastaId.ToString(),
attachment.ItemAnalisado.Arquivo.Id.ToString()));
stream.Write(dadosArquivo, 0, dadosArquivo.Length);
//stream.Position = 0;
stream.Close();
}
}
}
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
ms.Position = 0;
response.Content = new StreamContent(ms);
ms.Dispose();
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "teste.zip";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
return response;
}
我在尝试下载调用我的 API 的 Excel 文件时遇到此错误。我没有在这个方法上实现 ATP,所以一开始很困惑。这是我的初始代码:
public IHttpActionResult Get([FromUri] string id, [FromUri] SurveyReportRequestType requestType) {
byte[] excelData = System.Text.Encoding.UTF8.GetBytes("Boobs out!");
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
using (var stream = new MemoryStream(excelData))
{
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Data.xls"
};
}
return ResponseMessage(result);
}
经过思考并做出一些改变后,得出了以下结论:
...
byte[] excelData = System.Text.Encoding.UTF8.GetBytes("Boobs aout!");
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(excelData);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Data.xls"
};
return ResponseMessage(result);
...
如您所见,我将 stream 包含在 using 语句中。这导致请求未完成,因为内容不可用于响应。