1
Answer

Save array to disk

walid mousbah

walid mousbah

9y
501
1
Hi,
I have a function which retruns a pdf as a byte[]
private static CreatePDF()
{
byte[] pdf;
//Do some work & create pdf
return pdf;
}
If I call this function this way:
byte[] MyPdf = CreatePDF()
How can I save MyPdf to disk as a pdf file (c:\path\Filename.pdf) ?
(I'm using .NET 2)
Thanks
Answers (1)
0
Abrar Ahmad Ansari

Abrar Ahmad Ansari

NA 679 21.2k 9y
 
//First method
var filepath = Server.MapPath("~/Files/Filename.pdf");
using (MemoryStream stream = new MemoryStream(pdf))
{
FileStream fs = new FileStream(filepath, FileMode.Create,FileAccess.Write);
stream.WriteTo(fs);
}
 
//Second method
 
var filepath = Server.MapPath("~/Files/Filename.pdf");
FileStream fs = new FileStream(filepath, FileMode.Create,FileAccess.Write);
fs.WriteByte(pdf);