我正在尝试从 web api 下载 excel 文件。它只是一个 .net 框架 web api,返回类型为 IHttpActionResult。但是下载后会弹出文件格式无效的提示。当我保存在某个位置时,这个字节数组“fileContent”工作正常。仅当我将其作为 HttpResponse 返回时,下载后我无法打开该文件
byte[] fileContent = await _iFileService.DownloadFile(fileName);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(fileContent)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "abc.xlsx"
};
result.Content.Headers.ContentType = new
MediaTypeHeaderValue("application/octet-stream");
var response = ResponseMessage(result);
return response;