c:\windows\system32\inetsrv\Test.pdf 被拒绝
我需要使用可视化 Web 部件 sharepoint 2010 单击按钮生成 pdf 文档。 我正在使用开源库 http://www.itextpdf.com/ 。我能够使用项目类型作为 Windows 应用程序来执行以下代码。但是,当我想通过单击按钮执行此操作时,我收到 c:\windows\system32\inetsrv\Test.pdf' 被拒绝。错误。
下面是我正在使用的代码
public static void Main(string[] args) --Console Application
{
Console.WriteLine("PDF demo");
Document myDoc = new Document(PageSize.A4.Rotate());
try
{
PdfWriter.GetInstance(myDoc, new FileStream("Salman.pdf", FileMode.Create));
myDoc.Open();
myDoc.Add(new Paragraph("First pdf File made by Salman using Itext"));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.Message);
}
myDoc.Close();
}
}
,但我想对按钮单击事件执行相同的操作。
protected void pdfGenerator_OnClick(object sender, EventArgs e)
{
Document myDoc = new Document(PageSize.A4.Rotate());
try
{
PdfWriter.GetInstance(myDoc, new FileStream("Salman.pdf", FileMode.Create));---I get an error here
myDoc.Open();
myDoc.Add(new Paragraph("First pdf File made by Salman using Itext"));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.Message);
}
myDoc.Close();
}
}
请帮助我,我不明白错误的原因。我是第一次轮胎。
I have a requirement to generate a pdf document on a button click on using visual web part- sharepoint 2010.
I am using the the open source library http://www.itextpdf.com/ for the same. I am able to execute the below code using the project type as a Windowns Application. But, when I want do it on a button click , I am receiving c:\windows\system32\inetsrv\Test.pdf' is denied. error.
Below is the code that I am using
public static void Main(string[] args) --Console Application
{
Console.WriteLine("PDF demo");
Document myDoc = new Document(PageSize.A4.Rotate());
try
{
PdfWriter.GetInstance(myDoc, new FileStream("Salman.pdf", FileMode.Create));
myDoc.Open();
myDoc.Add(new Paragraph("First pdf File made by Salman using Itext"));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.Message);
}
myDoc.Close();
}
}
But I want do the same on a button click event.
protected void pdfGenerator_OnClick(object sender, EventArgs e)
{
Document myDoc = new Document(PageSize.A4.Rotate());
try
{
PdfWriter.GetInstance(myDoc, new FileStream("Salman.pdf", FileMode.Create));---I get an error here
myDoc.Open();
myDoc.Add(new Paragraph("First pdf File made by Salman using Itext"));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.Message);
}
myDoc.Close();
}
}
Please help me , i dont understand the reason for the error. I am tyring it for the first time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正尝试在您无权访问的目录中创建 PDF。这是因为你没有指定保存路径的目录;这导致它默认位于与当前工作目录相同的位置。在许多情况下,例如 ASP.NET;默认值与进程的位置相同。对于 ASP.NET,该进程为
w3wp.exe
,驻留在 c:\windows\system32\inetsrv 中。 w3wp 将作为(网络服务或 AppPoolIdentity)运行的大多数身份无法访问该目录。事实上,只有系统管理员才这样做。您需要将其保存在进程具有写入权限的其他位置:
应该是这样的:
如果您想将其保存在与网站相同的位置,则可以使用
HttpContext.Current.Server.MapPath
code>:无论如何,如果身份没有该目录的写入权限,则仍然需要该目录的写入权限。但你绝对不应该把它们放在 inetsrv 中。
The problem is you are trying to create a PDF in a directory you do not have access to. This is because you are not specifying the directory to save the path; which causes it to default in the same location as the current working directory. In many cases, such as for ASP.NET; the default is the same as the location of the process. For ASP.NET, the process is
w3wp.exe
and resides in c:\windows\system32\inetsrv. Most identities that w3wp will run as (Network Service or AppPoolIdentity) do not have access to that directory. In fact, only system administrators do.You need to save it somewhere else that the process has write permissions to here:
Should be something like:
If you want to save it in the same place as the website, you would use
HttpContext.Current.Server.MapPath
:Regardless, the identity will still need write permissions to the directory if it doesn't have them. But you definitely shouldn't put them in inetsrv.
只是不要使用需要服务器写入权限的“FileStream”。将其替换为 MemoryStream,然后将其内容刷新到用户的浏览器上。
Just don't use "FileStream" which requires write permissions on the server. Replace it with MemoryStream and then flush its content on the user's browser.