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

标签 c# asp.net response

我有以下代码:

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/

相关文章:

c# - 单声道 Thread.Sleep(int) 抛出 System.TypeLoadException

c# - Nopcommerce 更新实体问题

c# - 如何创建 ASP.NET 网页/站点项目?

c# - 测量上传时间?

ios - Objective c 从响应中获取参数值

oracle - 在空表中执行缓慢的查询。 (删除大量插入后)

c# - 为什么匿名委托(delegate)可以省略参数,而 lambda 不能?

asp.net - 从 asp.net 4 中的资源文件获取文本

c# - 在aspx.cs中创建onclick函数的c#代码

.net - 我们需要关闭 System.Net.WebRequest 的 ResponseStream 吗?