我有以下代码:
protected void Page_Load(object sender, EventArgs e)
{
byte[] buffer = null;
buffer = File.ReadAllBytes("C:\\myfile.pdf");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.OutputStream.Write(buffer, 0, buffer.Length);
HttpContext.Current.Response.End();
}
我想在页面加载来自的当前页面旁边为 pfd 文件打开第二个窗口。
最佳答案
为此,您需要将 PDF 上传到应用程序中可以呈现给用户的路径,然后注册一些 javascript 以在新窗口中打开 PDF:
protected void Page_Load(object sender, EventArgs e)
{
byte[] buffer = null;
buffer = File.ReadAllBytes("C:\\myfile.pdf");
//save file to somewhere on server, for example in a folder called PDFs inside your application's root folder
string newFilepath = Server.MapPath("~/PDFs/uploadedPDF.pdf");
System.IO.FileStream savedPDF = File.Create(newFilepath);
file.Write(buffer, 0, buffer.Length);
file.Close();
//register some javascript to open the new window
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenPDFScript", "window.open(\"/PDFs/uploadedPDF.pdf\");", true);
}
关于c# - 打开一个新窗口 ASP.net 代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398871/