开发者

ASP.NET - Sending a PDF to the user

I have a process that creates a PDF. I want these PDF's to be temporary and short lived. I want to be able to perform the following when the user clicks a button:

string CreatePDF()//returns fileName.pdf
PromptUserToDownloadPDF()
DeletePDF(fileName.pdf)

I want to avoid having to create a cleanup procedure and deal with any race conditions that arise开发者_C百科 from users concurrently creating PDF's while running cleanup.

In winforms, I would synchronously prompt a user to download a file. How can I do a similar task in web?

UPDATE

Please note that I am using a 3rd party app to create the PDF's (Apache FOP). Basically I (will) have a function that invokes the command line:

C:>fop "inputfile" "output.pdf"

So, in memory is not an option...that is unless I could somehow do like....

string CreatePDF()//returns fileName.pdf
string RecreatePDFInMemory()
DeletePDF(fileName.pdf)
PromptUserToDownloadPDF()


Something like this:

byte[] _pdfbytes = CreatePDF();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Length", _pdfbytes.Length.ToString());
Response.BinaryWrite(_pdfbytes);

Since this creates the PDF in memory, you don't need to worry about cleanup.

Edit for OP's edit:

From within CreatePDF, You can use Path.GetTempFileName to create a temp file and execute "fop" to write to that file. Delete that file immediately before returning the byte[]. I recommend doing this delete inside of a finally block. However, "Fop" does support having its output piped to stdout. Having the CreatePDF function grab that is probably cleaner.


Look into doing something along these lines.

Similar to what someone referred to in a different answer, you don't need to save the PDF file on your system, you can just send it as a response.

I'm not sure how you're creating your PDF, but try looking into this below and seeing if your process could use something like this.

HttpResponse currentResponse = HttpContext.Current.Response;
currentResponse.Clear();
currentResponse.ClearHeaders();
currentResponse.ContentType = "application/pdf";
currentResponse.AppendHeader("Content-Disposition", "attachment; filename=my.pdf");

//create the "my.pdf" here

currentResponse.Flush();
currentResponse.End();


Not sure of your process but you should be able to write the PDF to a byte[] and skip writing to the disk altogether.

byte[] pdf = GetPDFBytes(filename)
MemoryStream pdfStream = new MemoryStream(pdf);

Then use the pdfStream to send back to a user.


You can stream out a file with an asp.net page.

I tried to find very old article for you which demonstrates this with a GIF (there's not an actual file)

http://www.informit.com/articles/article.aspx?p=25487

It makes a special page which streams out the data (sets content type appropriately in the header).

Similarly, you can make a "page" to stream out the PDF - it might not even need to ever reside on disk, but if it did, you could delete it after streaming it to the browser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜