可以使用以下代码示例解决此问题。具体实现方法是将文件作为字节数组添加到响应体中,设置响应头Content-Disposition的attachment属性,并返回响应。
byte[] fileContents = // 从文件系统或其他地方读取文件的字节数组
string fileName = // 文件的名称,可以从文件元数据中获取或通过参数传递
HttpResponse response = Context.Response;
response.ContentType = "application/octet-stream"; // 设置响应体的内容类型为二进制文件
response.ContentLength = fileContents.Length;
response.Headers.Add("Content-Disposition", $"attachment; filename=\"{fileName}\""); // 设置文件下载时的名称
await response.Body.WriteAsync(fileContents, 0, fileContents.Length); // 将文件字节数组写入响应体
上一篇:Asp.net5webapiCustomActionResult
下一篇:Asp.net5应用程序部署失败并显示“Alternatively,installtheframeworkversion'5.0.0'”