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

Posted on 2008-02-04
Medium Priority
Last Modified: 2010-05-18
I have a asp.net 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.
Question by:mocaiv
  • 3
LVL 51

Accepted Solution

Ted Bouskill earned 2000 total points
ID: 20821105
Don't write the file to disk.  Use Response.OutputStream to write the file directly via the Response object to the HTTP stream.
LVL 51

Expert Comment

by:Ted Bouskill
ID: 20821112
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


Author Comment

ID: 20835209
what is document?

i have a function that returns io.memorystream
and theres no Save method for an insantiated memorystream
LVL 51

Expert Comment

by:Ted Bouskill
ID: 20837142

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question