在asp.net web API中,如何创建下载示例.xlsx文件的功能?

m528fe3b  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(121)

在asp.net web API中,如何创建一个下载示例.xlsx文件的功能?我尝试了下面的功能。

public HttpResponseMessage DownloadExcelFile()
    {
        string filePath = Directory.GetCurrentDirectory() + "/Modules/SampleFiles/sample3.xlsx";

        
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "sample3.xlsx"
        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        return response;
    }

字符串
我试过下面的

public HttpResponseMessage DownloadExcelFile()
    {
        string filePath = Directory.GetCurrentDirectory() + "/Modules/SampleFiles/sample3.xlsx";
        
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "sample3.xlsx"
        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        return response;
    }

olqngx59

olqngx591#

你试过使用下面的MediaType吗?

response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

字符串

相关问题