我怎样才能在ASP.NET Web请求返回一个PDF? [英] How can I return a pdf from a web request in ASP.NET?

查看:198
本文介绍了我怎样才能在ASP.NET Web请求返回一个PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说,我希望有人能够点击一个链接,并获得了一次性使用的PDF格式。我们有图书馆创建PDF文件,所以这不是一个问题。

Simply put, I'd like someone to be able to click a link, and get a one-time-use pdf. We have the library to create PDF files, so that's not an issue.

我们可以生成一个链接到aspx页面,有一个页面生成PDF,PDF格式保存到文件系统,然后Response.Redirect的到保存的PDF。那么我们无论如何都必须跟踪并清理PDF文件。

We could generate a link to an aspx page, have that page generate the pdf, save the pdf to the filesystem, and then Response.Redirect to the saved pdf. Then we'd somehow have to keep track of and clean up the PDF file.

由于我们没有永远需要保持这个数据,我想做相反,如果可能的话是什么,是有aspx页面生成PDF,全心全意直接追溯到原来的请求的响应。这可能吗?

Since we don't ever need to keep this data, what I'd like to do instead, if possible, is to have the aspx page generate the pdf, and serve it directly back as a response to the original request. Is this possible?

(在本例中,我们使用C#,我们要服务于PDF回来,但它似乎像任何解决方案应该会很适合各种.NET语言工作,返回文件类型)。

(In our case, we're using C#, and we want to serve a pdf back, but it seems like any solution would probably work for various .NET languages and returned filetypes.)

推荐答案

假设你可以得到一个字节[] 重新presenting您的PDF:

Assuming you can get a byte[] representing your PDF:

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
    "attachment;filename=\"FileName.pdf\"");

Response.BinaryWrite(yourPdfAsByteArray);

Response.Flush();

Response.End();

这篇关于我怎样才能在ASP.NET Web请求返回一个PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆