I have a aspx page which returns a pdf file with this code:
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=Order-" & Request("OrderNo") & ".pdf")
HttpContext.Current.Response.BinaryWrite(pdfContent)
HttpContext.Current.Response.End()
What modifications needs to be done to the code so the file is saved to the hard drive on the server? I guess BinaryWrite has to be replaced with something else, but what?
I'm very grateful for help!
Just specify the Path and the pdfcontent as parameters to the File.WriteAllBytes method as so:
File.WriteAllBytes(Server.MapPath("~/SubdirectoryInCurrentWebApp"),pdfContent);
You generally can't save any content outside your local application directory unless you configure the Application pool your application runs under with a user that has enough privileges to write to the destination directory. But out of the box, you should be able to write to subdirectories inside your web application.
You can write binary data to disk by calling File.WriteAllBytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With