gpt4 book ai didi

c# - 打开一个新窗口代码隐藏 ASP.net

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:06 26 4
gpt4 key购买 nike

我有以下代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com