- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一个 ASP.NET Core 2.2 项目,我需要使用浏览器下载我的 Excel,但是当我执行我的请求时,我只会得到一些 Json。
我的Excel在流中,而且流不是空的!
这是我的代码:
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
var streamContent = new StreamContent(stream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
streamContent.Headers.ContentDisposition.FileName = "Excel.xlsx";
message.Content = streamContent;
return message;
这是我得到的回复:
{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Type","value":["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]},{"key":"Content-Disposition","value":["attachment; filename=Excel.xlsx"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":null,"isSuccessStatusCode":true}
那么,有人知道如何使用 HttpResponseMessage 发送我的 Excel 文件吗?
我可以使用 Filesteam 下载我的 excel 文件,但我不想使用这种方式,因为我无法收到任何 Http 错误消息(错误请求、内部等)但如果有人知道我如何发送这样的消息并返回一个文件流,我将很高兴阅读你的建议!
编辑:这是我返回 FileStreamResult 时的代码
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
最佳答案
您可以从流中读取字节数组,并返回 FileContentResult。
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace DemoWebCore.Controllers
{
[Route("api/[controller]")]
public class FilesController : Controller
{
// GET api/files/sample.png
[HttpGet("{fileName}")]
public async Task<ActionResult> Get(string fileName)
{
var cd = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline")
{
FileName = "Excel.xlsx"
};
Response.Headers.Add("Content-Disposition", cd.ToString());
StreamContent stream = YOUR_STREAM_SOURCE
byte[] content = await stream.ReadAsByteArrayAsync();
return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
}
关于c# - 从 HttpResponseMessage 获取 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062814/
在 HttpClient 内部使用语句我需要从某个地方解包 HttpResponseMessage。 using (HttpClient client = new HttpClient()) {
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
var request = new HttpRequestMessage(HttpMethod.Get, $"api/Items"); request.Headers.Accept.Add(new M
我有一个方法接受 Stream 参数并将其传递给服务器 public async Task Execute(Stream archive) { archive.Seek(0,
我有如下服务的 MVC 项目: namespace comp.Services { public class CompService { public HttpClie
我已经验证了将 HttpResponseMessage 作为返回类型的文件设置为“编译”,就像他们在这里所说的:Why I cannot use HttpResponseMessage class?
我正在调用一个返回任务的方法,在调用方法中我需要读取字符串形式的响应。 这是我输入的代码: static public async Task Validate(string baseUri,HttpC
在等待新的 HttpClientFactory 解决 HttpClient 的所有问题时, 我仍然找不到是否应该处理 HttpResponseMessage 和 HttpRequestMessage
为什么要实现这个Web API [HttpGet("hello")] public HttpResponseMessage Hello() { var res = new HttpRespon
我正在编写一个 Web API Controller ,现在我有以下代码: public class PicklistsController : ApiController { private
我收到了 System.Net.Http.HttpResponseMessage> 类型的对象, 我如何获得 List ? 我尝试转换 content 属性并通过其 value 属性从 content
HttpResponseMessage r = new HttpResponseMessage(); r.StatusCode = HttpStatusCode.OK; r.Reaso
我正在尝试在我的 Web api 包装器中编写一个方法。我想利用“异步/等待”功能,这样用户界面就不会被阻塞。下面是 Web API 包装器中的代码片段。 public static asyn
我正在尝试从 .NET Core MVC 应用程序进行简单的 API 调用: using (var client = new HttpClient()) { client.BaseAddres
第一次使用.net的Httpclient,感觉很吃力。我已经设法调用服务器并从它接收响应,但一直停留在从响应中读取。这是我的代码: if (Method == HttpVerb.POST)
我想在 HttpResponseMessage 内容中返回一个文件,如下所示: return Request.CreateResponse(HttpStatusCode.OK, {{a file he
应用程序应该从 LoginUser() 接收到 httpresponsemessage 但它变得没有响应。 private void button1_Click(object sender,
我正在计算我的 C# web api 的性能。我写了一个非常简单的 HelloWorld 响应: public class HelloWorldController : ApiController {
我有一个 DelegatingHandler 的实现,它基本上接受一个请求并将其寻址到另一个服务器 ( http://localhost:9999/test/page.php --> http://o
我一直在开发移动应用程序的后端和前端,但在让我的帖子请求正常工作时遇到了问题。我一直在前端使用 Xamarin.Forms,在后端使用 .Net。 客户端代码: var res = await App
我是一名优秀的程序员,十分优秀!