In how to delete a pdf file you just created for a user to view

I have a page that generates a pdf file on the fly for a user and then immediately opens it for them in their adobe reader.  the problem is, I'm unable to delete it while they have the pdf open , because i get an access denied.   so now i basically have tons of pdf files in this directory that they are stored in, and wonder if there is any easy way to delete these pdf files upon a user viewing it.  

and no, i do not want to run a separate windows service or anything else that would clean the temp directory.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ted BouskillSenior Software DeveloperCommented:
Don't write the file to disk.  Use Response.OutputStream to write the file directly via the Response object to the HTTP stream.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ted BouskillSenior Software DeveloperCommented:
Assuming you have a document object in memory that holds your dynamically generated PDF the following code shows how to write it to the stream.
// clear http output
// set the content type to PDF
Response.ContentType = "application/pdf";
// add content type header
Response.AddHeader("Content-Type", "application/pdf");
// set the content disposition
Response.AddHeader("Content-Disposition", "inline;filename=Filename.pdf");
// write the buffer with pdf file to the output

Open in new window

mocaivAuthor Commented:
what is document?

i have a function that returns io.memorystream
and theres no Save method for an insantiated memorystream
Ted BouskillSenior Software DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.