[英]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.