如何在单击按钮时在iframe中显示pdfdoc [英] how to show pdfdoc in iframe when button is clicked
问题描述
我已编写代码我已将pdf文档下载到特定文件夹,并且我已将该文件夹路径作为iframe的源文件但是当单击按钮时pdfdoc正在下载到特定文件夹但未在iframe中显示
下面是代码,所以任何身体帮助我的问题是
使用系统;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.Security;
使用System.Web .UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web。 UI.WebControls.WebParts;
使用System.Xml.Linq;
使用System.Data;
使用System.Data.SqlClient;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用iTextSharp .text.html;
使用iTextSharp.text.html.simpleparser;
使用System.Text;
使用System.Net;
公共部分类dropdown_to_gridview:System.Web.UI.Page
{
protected void Page_Load(对象发件人,EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
GridViewBind();
}
public void GridViewBind( )
{
string cs =Data Source = HOME; Initial Catalog = Registration; Integrated Security = True;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(select * from employeep where Salary ='+ DropDownList1.SelectedValue +',con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnExportPDF_Click(object sender,EventArgs e)
{
// Response.ContentType =application / pdf;
// Response.AddHeader(content-disposition,
//attachment; filename = report.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
FileStream fs = new FileStream(@D:\Programs \ raj。 pdf,System.IO.FileMode.Create);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc =新文件(PageSize.A4,10f ,10f,10f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,fs);
pdfDoc.Open();
html parser.Parse(sr);
pdfDoc.Close();
fs.Close();
iframepdf.Attributes [src] = @D:\Programs\ raj.pdf;
}
public override void VerifyRenderingInServerForm(Control control)
{
/ *验证控件是否呈现* /
}
i have written code i have make the pdf doc to be download to a specific folder and that folder path i have given as source to iframe but when button is clicked pdfdoc is downloading to a specific folder but not showing in iframe
below is the code so any body helpme where my problem is
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.Net;
public partial class dropdown_to_gridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewBind();
}
public void GridViewBind()
{
string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + DropDownList1.SelectedValue + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
// Response.ContentType = "application/pdf";
// Response.AddHeader("content-disposition",
// "attachment;filename=report.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
FileStream fs = new FileStream(@"D:\Programs\raj.pdf", System.IO.FileMode.Create);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
iframepdf.Attributes["src"] = @"D:\Programs\raj.pdf";
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
推荐答案
首先,您的代码容易受到 SQL注入 [ ^ ]。
从不使用字符串连接来构建SQL查询。 总是使用参数化查询。
[ ^ ]
如何在没有技术术语的情况下解释SQL注入? |信息安全堆栈交换 [ ^ ]
SQL注入攻击机制Pluralsight [ ^ ]
其次,您要在服务器上保存PDF文件 。然后,您将服务器上文件的本地路径传递回客户端,并要求它显示该文件。
不起作用。客户端将尝试在其自己的D:
驱动器中打开该文件,该文件将因文件不存在而失败;或者,更有可能的是,浏览器将拒绝尝试从互联网站点打开iframe
中的本地文件。
您需要将iframe
指向服务器上的URL,该URL将生成PDF并将其发送回客户端。这样的事情应该有效:
Firstly, your code is vulnerable to SQL Injection[^].
NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
SQL injection attack mechanics | Pluralsight [^]
Secondly, you are saving the PDF file on the server. You are then passing the local path of the file on the server back to the client and asking it to display that file.
That will not work. Either the client will try to open the file in it's ownD:
drive, which will fail as the file doesn't exist; or, more likely, the browser will refuse to attempt to open a local file in aniframe
from an internet site.
You need to point theiframe
to a URL on your server which will generate the PDF and send it back to the client. Something like this should work:
public partial class dropdown_to_gridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["action"] == "export")
{
string salary = Request.QueryString["salary"];
GridViewBind(salary);
ExportPdf();
Response.End();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewBind(DropDownList1.SelectedValue);
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
iframepdf.Attributes["src"] = Request.Path + "?action=export&salary=" + HttpUtility.UrlEncode(DropDownList1.SelectedValue);
}
private void GridViewBind(string salary)
{
const string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
using (SqlConnection con = new SqlConnection(cs))
using (SqlCommand cmd = new SqlCommand("select * from employeep where Salary = @Salary", con))
{
cmd.Parameters.AddWithValue("@Salary", salary);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
private void ExportPdf()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=report.pdf");
StringWriter sw = new StringWriter();
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
}
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
pdfDoc.Open();
htmlparser.Parse(new StringReader(sw.ToString()));
pdfDoc.Close();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}
这篇关于如何在单击按钮时在iframe中显示pdfdoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!