如何在asp.net网络表单中名为“查看”的LinkBut​​ton上单击以在新选项卡中打开PDF附件?

时间:2019-05-27 10:31:47

标签: c# mysql asp.net webforms

每当我单击“查看链接”按钮时,它都会在同一页上打开pdf附件,我想在新标签页中打开它,并限制用户无论如何都无法下载它,请指导我。

当我单击视图时,链接按钮的方法调用此方法

protected void lnk_Click(object sender, EventArgs e)
            {
                GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;                   
                string DocumentCode = grdrow.Cells[1].Text;
                string TextInGrid = grdrow.Cells[0].Text;
                Regex re = new Regex("[;\\\\39/#:*?\"<>|&']");
                string AttachmentName = re.Replace(TextInGrid, " ");
                string DocID = grdrow.Cells[3].Text;double num;        
                if (double.TryParse(DocID, out num))
                {
                    byte[] bytes;
                    string fileName, ContentType, DocExtension;             
                    string constr = ConfigurationManager.ConnectionStrings["QMSConnectionString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {              
                  cmd.CommandText = "SELECT DivisionalDocID, AttachmentContent, 
                   AttachmentName,AttachmentContentType,DocExtension 
                   FROM DivisionalDocuments 
                   WHERE DocumentCode = @DocumentCode 
                   AND DivisionalDocID = @DocID";
                                cmd.Parameters.AddWithValue("@DocumentCode", DocumentCode);

                                cmd.Parameters.AddWithValue("@DocID", DocID);
                                cmd.Connection = con;
                                con.Open();
                                using (SqlDataReader sdr = cmd.ExecuteReader())
                                {
                                    sdr.Read();                            
                                    fileName = sdr["AttachmentName"].ToString();
                                    FileNameWithOutCharchters = fileName.Replace(",", "");
                                    ContentType = sdr["AttachmentContentType"].ToString();

                                    DocExtension = sdr["DocExtension"].ToString();

                                }
                                con.Close();//close the connection
                            }
                        }
                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "";                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.ContentType = ContentType; 
        //i use inline here to so the attachment will not be 
        //downloaded
//   You really posted only code, without a line to explain what you are trying to achieve, why it doesn't work, and which error messages you eventually got.            
          Response.AppendHeader("Content-Disposition", "inline; filename=" + FileNameWithOutCharchters + DocExtension);
                        Response.BinaryWrite(bytes);
                          Response.Flush();
                        Response.End();
                    }
                    else
                    {
                    }

谢谢

0 个答案:

没有答案