ASP.NET - 发送PDF用户 [英] ASP.NET - Sending a PDF to the user
问题描述
我有一个创建PDF的过程。我希望这些PDF文件是暂时的和短暂的。我希望能够执行下列操作,当用户点击一个按钮:
字符串CreatePDF()//返回fileName.pdf
PromptUserToDownloadPDF()
DeletePDF(fileName.pdf)
我想避免创建净化程序和处理来自用户同时创建PDF的同时运行清理出现的任何竞争条件。
在的WinForms,我会同步提示用户下载文件。我怎样才能做一个类似任务的网站的?
更新
请注意,我使用的是第三方应用程序来创建PDF文件(的Apache FOP )。基本上我(会)有调用命令行功能:
C:> FOPinputfile中,输出.pdf
块引用>所以,记忆不是一个选项...这是除非我能以某种方式做喜欢....
字符串CreatePDF()//返回fileName.pdf
串RecreatePDFInMemory()
DeletePDF(fileName.pdf)
PromptUserToDownloadPDF()
解决方案事情是这样的:
字节[] = _pdfbytes CreatePDF();
Response.ContentType =应用程序/ PDF
Response.AppendHeader(内容长度,_pdfbytes.Length.ToString());
Response.BinaryWrite(_pdfbytes);由于这在内存中创建PDF,您不必担心清理工作。
修改为OP的编辑:
这在
CreatePDF
,您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename.aspx\"><$c$c>Path.GetTempFileName$c$c>创建一个临时文件并执行花花公子写入该文件。删除文件立即返回字节之前[]
。我建议这样做删除最后
区块内。不过,Fop的不支持有其管道输出到标准输出。具有CreatePDF
函数抢这可能是更清洁。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 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 usePath.GetTempFileName
to create a temp file and execute "fop" to write to that file. Delete that file immediately before returning thebyte[]
. I recommend doing this delete inside of afinally
block. However, "Fop" does support having its output piped to stdout. Having theCreatePDF
function grab that is probably cleaner.这篇关于ASP.NET - 发送PDF用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!