我试图在用户指定时显示在我的网站上下文中显示的PDF页面。我有以下代码:我怎么能写一个PDF到一个asp.net页面?
if (context.User.Identity.IsAuthenticated)
{
string SampleURL = context.Request.CurrentExecutionFilePath; //CurrentExecutionFilePath;
context.Response.Buffer = true;
context.Response.Clear();
using (FileStream fs = new FileStream(SampleURL,FileMode.Open)) //System.IO.File.OpenRead(path))
{
int length = (int)fs.Length;
byte[] buffer;
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(buffer);
context.Response.End();
}
}
else
{
context.Response.Redirect(
"~/Error/invalid_access.aspx");
}
唯一的问题是我无法让PATH正常工作。 如果我直接通过URL调用PDF,它将是http://www.abc.com/reports/sample.pdf,但我无法让自己回到那个位置。 我实现了一个HTTPHandler来防止有人转到URL,但现在我需要将文件传回浏览器并写入它。
想法,意见,建议?
编辑: 它的PATH,我无法得到相对网址指向正确的位置。 context.Request.CurrentExecutionFilePath给了我“/www.abc.com/sample_reports/sample.pdf”,但我似乎无法扭转,以便能够打开/阅读它
实际上它只是Server.MapPath正在把我扔掉。我们有另一个'服务器'命名空间ye'olde intellisense没有给我这个.MapPath – 2011-03-02 01:47:41
只是FYI,很长一段时间扔我的东西是〜符号。 〜在Server.MapPath中不起作用。花了我多年的时间来弄清楚。哈哈 – 2011-03-02 03:11:33