Reputation: 2169
How do i generate a temporal PDF in my ASP.NET application so I get an temp URL for a generated PDF from an array of bytes ?
I use this but i think it could get simple:
//bytes[] arrayPDF exists before
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
File.WriteAllBytes(fileName, arrayPDF);
HtmlGenericControl obj = new HtmlGenericControl("embed");
obj.Attributes.Add("src", fileName);
obj.Attributes.Add("style","border-radius: 10px;position: relative;top:0;right:0;left:0;bottom:0;width:100%;height:620px;");
obj.Attributes.Add("height","600");
obj.Attributes.Add("type", "application/pdf");
form1.Controls.Add(obj);
My main problem is how to generate a temp file that i am sure that it would not stay in the server more than the request/show time
Upvotes: 0
Views: 1268
Reputation: 1048
Use an ASP.NET ASHX Handler:
public class Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
// relevant context.Response lines
}
public bool IsReusable
{
get { return false; }
}
}
Also check this answer:
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = "application/pdf";
Stream fileStream = publishBookManager.GetFile(documentId);
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
context.Response.BinaryWrite(data);
context.Response.Flush();
Upvotes: 1