Stack trace sembraPerché sto ottenendo "Impossibile accedere a un flusso chiuso" qui?
[ObjectDisposedException: Impossibile accedere un flusso chiuso.]
System.IO .__ Error.StreamIsClosed() +53
System.IO.MemoryStream.Read (Byte [] tampone, Int32 Offset, conteggio Int32) 11.411.219 System.Web.Mvc.FileStreamResult.WriteFile (HttpResponseBase risposta) +81 System.Web.Mvc.FileResult.ExecuteResult (contesto ControllerContext) 168
System.Web.Mvc.ControllerActionInvoker. InvokeActionResult (ControllerContext controllerContext, ActionResult actionResult) +13
dopo aver richiamato
//Byte[] bytes;
using (var ms = new MemoryStream())
{
using (var doc = new Document())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
//var example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "template.html"));
var example_css = @".headline{font-size:200%}";
using (var srHtml = new StringReader(example_html))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
doc.Close();
}
}
//bytes = ms.ToArray();
return File(ms, "application/pdf", "Test.pdf");
}
ho letto MemoryStream - Cannot access a closed Stream, ma non è lo stesso scenario, perché non sto usando StreamReader
Edit: Ancora non funziona con
[OutputCache(NoStore = true, Duration = 0)]
public ActionResult Run()
{
Byte[] bytes;
var ms = new MemoryStream();
try
{
using (var doc = new Document())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
writer.CloseStream = false;
doc.Open();
var example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
//var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "LinkEmailTemplate.html"));
var example_css = @".headline{font-size:200%}";
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
doc.Close();
}
}
bytes = ms.ToArray();
ms.Position = 0;
return File(ms, "application/pdf", "Test.pdf");
}
catch
{
ms.Dispose();
throw;
}
}
Perché lo stream è stato chiuso non appena sei uscito dal metodo di azione. Quando la vista ha tentato di accedervi, ha scoperto che era già chiusa. –
'Ancora non funziona con' - define 'not working'. Inoltre hai una variabile chiamata 'bytes' che non fa nulla ma sono curioso di sapere se c'è qualcosa in questo array prima che il tuo metodo esca. – Igor
* Perché * si copia il contenuto del flusso in un array di byte? Se lo fai, non c'è motivo di restituire * stream * con 'File (..)', basta restituire l'array di byte es. 'File (byte ...)' –