将.aspx页面转换为pdf“访问路径/ C拒绝错误”时显示 [英] While converting .aspx page to pdf "access to path /C denied error" displayed

查看:69
本文介绍了将.aspx页面转换为pdf“访问路径/ C拒绝错误”时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 源代码 -



< asp:Panel ID =pnlPersonrunat =server>





发票表格



--source code--

<asp:Panel ID="pnlPerson" runat="server">


Invoice Form








< asp:Image ID =Image1runat = serversrc =invoice.pngwidth =100pxheight =100px/>



发票号码:123


创建:< asp:Label ID =Label9runat =server>


到期日:

< asp:Label ID =Label10runat =server>







< asp:Button ID =Exporttopdfrunat =serverText =Save as PDFOnClick =Exporttopdf_Click/>













- 代码落后 -



protected void Exporttopdf_Click(object sender,EventArgs e)

{

Response.ContentType =application / pdf;

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

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

pnlPerson.RenderControl(hw);

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

文档pdfDoc =新文档(PageSize.A4,10f,10f,100f,0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc,Response.OutputStream);

pdfDoc.Open();

htmlparser。解析(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();



}

public override void VerifyRenderingInServerForm(Control control)

{



}



--error--



访问路径'C :\'被拒绝。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.UnauthorizedAccessException:拒绝访问路径C:\。



ASP.NET无权访问所请求的资源。考虑将资源的访问权限授予ASP.NET请求标识。 ASP.NET具有基本进程标识(在IIS 5上通常为{MACHINE} \ ASPNET或在IIS 6上为网络服务),如果应用程序未模拟,则使用该标识。如果应用程序通过< identity impersonate =true/>进行模拟,则标识将是匿名用户(通常为IUSR_MACHINENAME)或经过身份验证的请求用户。



要授予ASP.NET文件访问权限,请在资源管理器中右键单击该文件,选择属性,然后选择安全选项卡。单击添加以添加适当的用户或组。突出显示ASP.NET帐户,并选中所需访问的框。



来源错误:





第246行:PdfWriter.GetInstance(pdfDoc,Response.OutputStream);

第247行:pdfDoc.Open();

第248行:htmlparser.Parse(sr);

第249行:pdfDoc.Close();

第250行:Response.Write(pdfDoc);



我尝试了什么:



帮我解决问题..如何恢复这个



在此先感谢!!



<asp:Image ID="Image1" runat="server" src="invoice.png" width="100px" height="100px" />

Invoice No: 123

Created:<asp:Label ID="Label9" runat="server" >

Due:
<asp:Label ID="Label10" runat="server" >



<asp:Button ID="Exporttopdf" runat="server" Text="Save as PDF" OnClick="Exporttopdf_Click" />






--code behind--

protected void Exporttopdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}
public override void VerifyRenderingInServerForm(Control control)
{

}

--error--

Access to the path 'C:\' is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:


Line 246: PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Line 247: pdfDoc.Open();
Line 248: htmlparser.Parse(sr);
Line 249: pdfDoc.Close();
Line 250: Response.Write(pdfDoc);

What I have tried:

Help me solve the issue..how to recover this

Thanks in advance!!

推荐答案

您必须创建一个带路径的文件流你有写访问权并将该流传递给 PdfWriter.GetInstance()而不是 Response.OutputStream

You have to create a file stream with a path where you have write access and pass that stream to PdfWriter.GetInstance() instead of Response.OutputStream:
string pdfPath = "c:\some-path\Panel.pdf";
PdfWriter.GetInstance(pdfDoc, new FileStream(pdfPath, FileMode.Create));

取决于您的设置 pdfPath 可以从服务器映射路径,临时目录或服务器文件系统上的绝对路径构建。

Depending on your setup pdfPath may be build from a server map path, a temporary directory, or an absolute path on the server's file system.


这篇关于将.aspx页面转换为pdf“访问路径/ C拒绝错误”时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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