Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - save pdf file on server

Tags:

asp.net

vb.net

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!

like image 544
user1898347 Avatar asked Mar 08 '26 07:03

user1898347


2 Answers

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.

like image 187
Icarus Avatar answered Mar 09 '26 23:03

Icarus


You can write binary data to disk by calling File.WriteAllBytes.

like image 22
SLaks Avatar answered Mar 09 '26 22:03

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!