从HttpResponseMessage获取Excel文件 [英] Get Excel file from HttpResponseMessage

查看:212
本文介绍了从HttpResponseMessage获取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ASP.NET Core 2.2项目,我需要使用浏览器下载Excel,但是当我执行请求时,我只会得到一些Json.

I am working on a ASP.NET Core 2.2 project and I need to download my Excel with my browser, but when I am doing my request, I just get some Json.

我的Excel在流中,并且流不为空!

My Excel is in the stream, and the stream is not empty !

这是我的代码:

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文件吗?

So, do someone know how I can send my Excel file with HttpResponseMessage ?

我可以使用Filesteam下载我的excel文件,但是我不想使用这种方式,因为我无法收到任何Http错误消息(错误请求,内部错误等).但是,如果有人知道我如何发送这样的消息并返回Filestream,我将很高兴阅读您的建议!

I can download my excel file using Filesteam, but I don't want to use that way because I can't get any Http error message (Bad request, Internal, etc.) But if someone know how I can send message like this and returning a Filestream, I will be glad to read your advice !

这是我返回FileStreamResult时的代码

EDIT : Here is the code when I am returning a FileStreamResult

                return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

推荐答案

您可以从Stream读取为Byte数组,并返回FileContentResult.

You could read as Byte array from Stream, and return 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");
        }
    }
}

这篇关于从HttpResponseMessage获取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆