C# 获取错误“~\文件名.pdf未作为文件或资源找到。”;
我有一个从报表服务器生成报表的网站。我需要通过URL将参数传递给报表服务器 这就是我得到的回应。然后,我创建了一个本地文件流来存储响应中的数据。但问题是,当我试图访问本地文件以便与另一个文件合并时,它会返回错误 “D:\Work\Report\Reporting Site 31012017\DAnalytics.Web\reports\147b9b1f-bb42-4353-bda2-490fb8c6a026.pdf未作为文件或资源找到” 这就是我通过URL传递参数并创建本地文件的方式C# 获取错误“~\文件名.pdf未作为文件或资源找到。”;,c#,asp.net,pdf,reporting-services,ssrs-2008-r2,C#,Asp.net,Pdf,Reporting Services,Ssrs 2008 R2,我有一个从报表服务器生成报表的网站。我需要通过URL将参数传递给报表服务器 这就是我得到的回应。然后,我创建了一个本地文件流来存储响应中的数据。但问题是,当我试图访问本地文件以便与另一个文件合并时,它会返回错误 “D:\Work\Report\Reporting Site 31012017\DAnalytics.Web\reports\147b9b1f-bb42-4353-bda2-490fb8c6a026.pdf未作为文件或资源找到” 这就是我通过URL传递参数并创建本地文件的方式
public string MergeDocs(Dictionary<int, string> _Files)
{
string _CombinedPDFPath = string.Empty;
if (_Files.Count > 0)
{
_CombinedPDFPath = Path.Combine(Path.GetDirectoryName(_Files[0]), "DR_" + DateTime.Now.ToString("ddMMHHmmss") + ".pdf");
//Step 1: Create a Docuement-Object
Document document = new Document();
try
{
//Step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_CombinedPDFPath, FileMode.Create));
//Step 3: Open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int n = 0;
int rotation = 0;
//Loops for each file that has been listed
for (int iCount = 0; iCount < _Files.Count; iCount++)
{
//The current file path
// string filePath = sourcefolder + filename;
// we create a reader for the document
PdfReader reader = new PdfReader(_Files[iCount]);
//Gets the number of pages to process
n = reader.NumberOfPages;
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(1));
document.NewPage();
//Insert to Destination on the first page
if (i == 1)
{
Chunk fileRef = new Chunk(" ");
fileRef.SetLocalDestination(_Files[iCount]);
document.Add(fileRef);
}
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
}
}
catch (Exception e) { throw e; }
finally
{
document.Close();
//AddIndex(_CombinedPDFPath);
}
}
return _CombinedPDFPath;
}
公共字符串合并文档(字典文件)
{
string _CombinedPDFPath=string.Empty;
如果(_Files.Count>0)
{
_CombinedPDFPath=Path.Combine(Path.GetDirectoryName(_Files[0]),“DR_u”+DateTime.Now.ToString(“ddMMHHmmss”)+“.pdf”);
//步骤1:创建文档对象
文档=新文档();
尝试
{
//步骤2:我们创建一个侦听文档的编写器
PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(_CombinedPDFPath,FileMode.Create));
//步骤3:打开文档
document.Open();
PdfContentByte cb=writer.DirectContent;
PDF导入页面;
int n=0;
整数旋转=0;
//列出的每个文件的循环
对于(int-iCount=0;iCount<\u Files.Count;iCount++)
{
//当前文件路径
//字符串filePath=sourcefolder+filename;
//我们为文档创建一个阅读器
PdfReader reader=新的PdfReader(_Files[iCount]);
//获取要处理的页数
n=reader.NumberOfPages;
int i=0;
而(i
这是我用来合并文档的代码
有人知道为什么会发生此错误以及如何解决此错误吗
非常感谢所有帮助。文件是否存在于该路径中?
如果是这样,是否可能是用另一个用户帐户写入的,因此读取器没有读取权限?该路径中是否存在该文件?
如果是这样的话,它可能是用另一个用户帐户写的,这样读者就没有阅读权限了吗?当我收到HTTP请求的响应时,我在本地创建了这个pdf文件。这个文件是自动生成的(你可以在代码中看到它)。我的用户帐户具有读取权限。这可能是因为您从未关闭并处置您在PdfWriter中使用的文件流造成的吗
PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(_CombinedPDFPath,FileMode.Create))当我从HTTP请求得到响应时,我在本地创建了这个pdf文件。这个文件是自动生成的(你可以在代码中看到它)。我的用户帐户具有读取权限。这可能是因为您从未关闭并处置您在PdfWriter中使用的文件流造成的吗PdfWriter writer=PdfWriter.GetInstance(文档,新文件流(_CombinedPDFPath,FileMode.Create))代码>愚蠢的问题,但文件是否确实存在于目录中?还有,什么是抛出异常?是不是Path.GetDirectoryName(_Files[0])
?创建了该文件并向文档中添加了20页。但该文件不在目录中。愚蠢的问题,但该文件确实在目录中吗?还有,什么是抛出异常?是不是Path.GetDirectoryName(_Files[0])
?创建了该文件并向文档中添加了20页。但该文件不在目录中。
public string MergeDocs(Dictionary<int, string> _Files)
{
string _CombinedPDFPath = string.Empty;
if (_Files.Count > 0)
{
_CombinedPDFPath = Path.Combine(Path.GetDirectoryName(_Files[0]), "DR_" + DateTime.Now.ToString("ddMMHHmmss") + ".pdf");
//Step 1: Create a Docuement-Object
Document document = new Document();
try
{
//Step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_CombinedPDFPath, FileMode.Create));
//Step 3: Open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int n = 0;
int rotation = 0;
//Loops for each file that has been listed
for (int iCount = 0; iCount < _Files.Count; iCount++)
{
//The current file path
// string filePath = sourcefolder + filename;
// we create a reader for the document
PdfReader reader = new PdfReader(_Files[iCount]);
//Gets the number of pages to process
n = reader.NumberOfPages;
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(1));
document.NewPage();
//Insert to Destination on the first page
if (i == 1)
{
Chunk fileRef = new Chunk(" ");
fileRef.SetLocalDestination(_Files[iCount]);
document.Add(fileRef);
}
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
}
}
catch (Exception e) { throw e; }
finally
{
document.Close();
//AddIndex(_CombinedPDFPath);
}
}
return _CombinedPDFPath;
}