在 ASP.NET webform 的新窗口中打开文件

问题描述

在我的 ASP.NET 网络表单中,我有这个功能来下载我从数据库获取文件。 请如何更改它以在新的浏览器窗口中显示文件?它主要是 pdf 或图像。

        protected void DownloadFile(object sender,EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        byte[] bytes;
        string fileName;
        //string contentType;
        string constr = WebConfigurationManager.AppSettings["inputsqlCon"];
        using (sqlConnection con = new sqlConnection(constr))
        {
            using (sqlCommand cmd = new sqlCommand())
            {
                cmd.CommandText = @"SELECT ATTACH_NAME,ATTACH
                FROM PA_REQ_NOTE_ATTACHMENTS 
                where ATTACH_ID = @Id";
                cmd.Parameters.AddWithValue("@Id",id);
                cmd.Connection = con;
                con.open();
                using (sqlDataReader sdr = cmd.ExecuteReader())
                {
                    sdr.Read();
                    bytes = (byte[])sdr["ATTACH"];
                    //contentType = sdr["ContentType"].ToString();
                    fileName = sdr["ATTACH_NAME"].ToString();
                }
                con.Close();
            }
        }
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //Response.ContentType = contentType;
        Response.AppendHeader("Content-disposition","attachment; filename=" + fileName);
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)