我试图将gridview数据导出到asp.net中的pdf时发生错误 [英] Error occurred when im trying to export the gridview data to pdf in asp.net

查看:57
本文介绍了我试图将gridview数据导出到asp.net中的pdf时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 如果我点击按钮(btnConverttoPDF)  form 
itwill将创建包含表单内所有内容的pdf ..
it ' s工作正常本地..

但是当应用程序部署在服务器中然后
它'
引发错误.. as 跟随



无法建立连接,因为目标机器主动拒绝它127.0.0.15:55374

WebException:无法连接到删除服务器

并提及文件的路径,如
E:\ Main \ OnlineSite \OnlineBanking\ClosingBalance.aspx.cs





我在这里注意到的是127.0.0.15是我的IP和55374是我开发应用程序的portno



我发现了问题.. 

< asp:Image ID = Image1 ImageUrl = http:// localhost:55374 / img / bank_logo.png runat = server >

上述控件导致问题..
htmlparser.Parse(sr);

我可以知道如何解决它?





我尝试了什么:



protected void btnconvert_Click(object sender,EventArgs e)

{

using(StringWriter sw = new StringWriter())

{

使用(HtmlTextWriter hw = new HtmlTextWriter(sw))

{

//导出所有页面

grdLast10trans.AllowPaging = false;

//调用函数,这里我将数据添加到数据源并在网格中进行双击
BindGrid();

content.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

文件pdfDoc =新文件( PageSize.A3,10f,10f,10f,10f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);



PdfWriter.GetInstance( pdfDoc,Response.OutputStream);

pdfDoc.Open();

//pdfDoc.NewPage();

htmlparser.Parse(sr); //这里发生错误

pdfDoc.Close();

Response.ContentType =application / pdf;

Response.AddHeader( content-disposition,attachment; filename = GridViewExport.pdf);

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Write(pdfDoc);

HttpContext.Current.ApplicationInstance.CompleteRequest();

}

}

}

解决方案

http:// localhost:55374 / img / bank_logo.png 是一个绝对的URL,带有逻辑故障...... localhost总是指网站托管的机器...

要解决它,请使用相对URL,如 /img/bank_logo.png ,这是相对于你的网站的根(/),应该在每个主机上工作...



(关于你的具体问题...在开发机器上你是使用IIS Express - 或Cassini用于较旧的VS - 并且它将从更高范围的可用端口动态选择端口。在普通主机上,您将使用标准80 - 443 - 端口用于HTTP,并指定端口将导致错误...)

if i onclick of a button(btnConverttoPDF) in some form 
itwill create pdf with all the content inside the form..
it's working fine locally..

But when the application is deployed in server then 
it's raising an error.. as follows



"No connection could be made because the target machine actively refused it 127.0.0.15:55374"

WebException: unable to connect to the remove server

and mentioning the path of the file like
E:\Main\OnlineSite\OnlineBanking\ClosingBalance.aspx.cs



What i noticed here is 127.0.0.15 is my IP and 55374 is my portno where i developed the app

I found the issue.. 

<asp:Image ID="Image1" ImageUrl="http://localhost:55374/img/bank_logo.png" runat="server">

the above control is causing an issue.. during 
htmlparser.Parse(sr);

May i know How can i resolve it?



What I have tried:

protected void btnconvert_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To Export all pages
grdLast10trans.AllowPaging = false;
//Calling a Function, here im adding the data to the Datasource and diplaying in Grid
BindGrid();
content.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A3, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//pdfDoc.NewPage();
htmlparser.Parse(sr); //Error Occurring here
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}

解决方案

http://localhost:55374/img/bank_logo.png is a absolute URL, with a logical glitch...localhost refers always the machine on which the site hosted...
To resolve it use relative URL like /img/bank_logo.png, which is relative to the root (/) of your site and should work from every host...

(As for your specific problem...on the development machine you are using IIS Express - or Cassini for older VS - and it will choose the port dynamically from the higher range of available ports. On a normal host you will use the standard 80 - 443 - port for HTTP, and specifying the port will lead to error...)


这篇关于我试图将gridview数据导出到asp.net中的pdf时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆