I've got a very strange problem when trying to write an ASHX response. I use it in my web project to allow users to download a generated .xlsx file. First I have a response.redirect from my main page to the .ashx file, and in the .ashx file I generate an excel file and sends it to the response.
This works perfectly when run in Microsoft Visual Studio - My coding envierment, but doesnt work when I export it to the IIS 8.5 server. There all I get is a white page instead of a xlsx file. Ive tried both Response.end and CompleteRequest. If anyone have a clue, please help. (WriteExcelFile works, since it generates a perfect file in coding envierment)
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
System.Web.HttpRequest Request = System.Web.HttpContext.Current.Request;
//Remove block
System.Data.DataTable myExcelData = new System.Data.DataTable();
myExcelData.Columns.Add("Tid", typeof(string));
myExcelData.Columns.Add("Grupp", typeof(string));
myExcelData.Columns.Add("Felkod", typeof(Int32));
myExcelData.Columns.Add("FelText", typeof(string));
myExcelData.Columns.Add("Kommentar", typeof(string));
myExcelData.Columns.Add("Kommentar2", typeof(string));
myExcelData.Columns.Add("Severity", typeof(Int32));
myExcelData.AcceptChanges();
myExcelData.Rows.Add( "STARTTID",
"FELTYP",
17,
"HEJSAN",
"NO COMMENT",
"NO COMMENT",
23);
myExcelData.AcceptChanges();
System.Data.DataSet ds = new System.Data.DataSet();
ds.Tables.Add(myExcelData);
//Remove Memorystream block
System.IO.MemoryStream stream = new System.IO.MemoryStream();
using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
{
ExportToExcel.CreateExcelFile.WriteExcelFile(ds, document, "AlarmList");
}
stream.Flush();
stream.Position = 0;
Response.ClearContent();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
// NOTE: If you get an "HttpCacheability does not exist" error on the following line, make sure you have
// manually added System.Web to this project's References.
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.AddHeader("content-disposition", "attachment; filename=" + "header.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
byte[] data1 = new byte[stream.Length];
stream.Read(data1, 0, data1.Length);
stream.Close();
Response.BinaryWrite(data1);
Response.Flush();
Response.End();
// System.Web.HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
// System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events
//Response kastar alltid exception .end exekveras inte. så vi kör linjen ovan istellet
Aucun commentaire:
Enregistrer un commentaire