简体   繁体   English

WebAPI 2 exports Excel,PDF that corupted when deploy on IIS 7/8 but work normally in IIS 10 on my local machine

[英]WebAPI 2 exports Excel,PDF that corupted when deploy on IIS 7/8 but work normally in IIS 10 on my local machine

WebAPI 2 exports Excel,PDF that corupted when deploy on IIS 7/8 but work normally in IIS 10 on my local machine. WebAPI 2 exports Excel,PDF that corupted when deploy on IIS 7/8 but work normally in IIS 10 on my local machine. Here is the excel file when I open it enter image description here这是 excel 文件,当我打开它时在此处输入图像描述

Here is my code to render excel这是我渲染 excel 的代码

[HttpPost]
    [Route("api/ThiSinhDuThi/ExportExcel")]
    public HttpResponseMessage ExportExcel()
    {

        string filepath = HttpContext.Current.Server.MapPath("~/Excel_Template/BieuDSTSDuThi.xls");
        //FileInfo template = new FileInfo(filepath);
        HSSFWorkbook hssfwb;
        using ( FileStream file = new FileStream(filepath, FileMode.Open, FileAccess.Read) )
        {
            hssfwb = new HSSFWorkbook(file);
            file.Close();
        }
       
        using ( var memoryStream = new MemoryStream() ) //creating memoryStream
        {
            hssfwb.Write(memoryStream);
          
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(memoryStream.ToArray())
            };
            
            response.Content.Headers.ContentType = new MediaTypeHeaderValue
                   ("application/vnd.ms-excel");
            response.Content.Headers.ContentDisposition =
                   new ContentDispositionHeaderValue("attachment")
                   {
                       FileName = "DSThiSinhDuThi.xls"
                   };
            
            return response;
        }
        //return stream;


    }

That seems too human readable for xls...maybe it's supposed to be.xlsx to open properly...though you could upload a file somewhere to check maybe?对于 xls 来说,这似乎太人类可读了……也许它应该是 .xlsx 才能正确打开……尽管您可以将文件上传到某处进行检查?

Or if that is a response, which it probably more is, then try to see if the attachment part which isn't visible in the browser once copied in an xls file works...或者,如果这是一个响应,它可能更多,然后尝试查看在 xls 文件中复制后在浏览器中不可见的附件部分是否有效......

I suspect you're getting a content that's not supposed to be be used directly from a browser but some client app maybe or it's getting serialized when it shouldn't be into JSON on IIS7/8 (ie the error is more likely up stream in the web server setup in some output formatter than in that code)?我怀疑你得到的内容不应该直接从浏览器使用,但某些客户端应用程序可能会被序列化,或者当它不应该进入 IIS7/8 上的 JSON 时(即错误更有可能出现在 stream 中)某些 output 格式化程序中的 web 服务器设置比该代码中的)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将WebAPI(aspnetcore 2)部署到IIS - Deploy WebAPI (aspnetcore 2) to IIS 我的asmx服务在我的本地机器的iis上运行正常,即win10。 但不适用于Windows Server 2008 r2 - my asmx service works fine on my local machine's iis which is win10. but does not work on windows server 2008 r2 .NET Core Web API IIS部署本地计算机 - .NET Core Web API IIS deploy local machine 带有 Windows 身份验证的 IIS 网站无法在本地 Windows 10 计算机上运行 - IIS website with windows authentication does not run on local Windows 10 machine ASP.NET 项目在 VS 运行时在本地运行,但不在本地计算机或服务器上的 IIS 上运行 - ASP.NET project works locally on build when VS is running but not on IIS on my local machine or my server 我的WebApi在Visual Studio上运行良好,但在IIS上不起作用 - My WebApi works fine on Visual Studio but doesn't work on IIS 代码在本地机器上工作,但在 IIS 上不工作 - Code Works On Local Machine But Not On IIS 提供程序未在IIS中的本地计算机上注册 - Provider is not registered on the local machine in IIS 如何在我的本地应用程序中启用远程访问 - IIS Windows 10 - How to enable remote access in my local application - IIS Windows 10 在本地IIS上部署后启动Process.Start时访问被拒绝 - Access Denied,when Process.Start after Deploy on Local IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM