【问题标题】:getting error "~\filename.pdf not found as file or resource."收到错误“~\filename.pdf 找不到文件或资源。”
【发布时间】:2017-06-13 06:25:54
【问题描述】:

我有一个网站可以从报表服务器生成报表。我需要通过 URL 将参数传递给报表服务器。

这就是我得到的回应。之后,我创建了一个本地文件流来存储响应中的数据。但问题是当我尝试访问本地文件以与另一个文件合并时它返回错误

“D:\Work\Report\Reporting Site 31012017\DAnalytics.Web\reports\147b9b1f-bb42-4353-bda2-490fb8c6a026.pdf 找不到文件或资源”

    void GenerateMinMaxWebReport(GenerateReportArgs args)
            {
                long totalBytesRead = 0;
                const int blockSize = 65536;
                Byte[] buffer = new Byte[blockSize];

            StringBuilder _url = new 

StringBuilder(ConfigurationManager.AppSettings["SSRS_SERVER"])
            .Append("?").Append(ConfigurationManager.AppSettings["SSRS_PATH"])
            .Append("dailyreport_minmax&rs:Command=Render&rs:format=PDF&rc:Toolbar=False&rc:Parameters=False")
            .Append("&ReportID=").Append(hdnReportID.Value);
        if (args.FromDate.HasValue)
            _url.Append("&FromDate=").Append(args.FromDate.Value.ToString("MM/dd/yyyy"));
        if (args.ToDate.HasValue)
            _url.Append("&ToDate=").Append(args.ToDate.Value.ToString("MM/dd/yyyy"));
        _url.Append("&DoAutoPick=").Append(args.DoAutoPick.ToString());
            _url.Append("&DoSummary=").Append(args.DoSummary.ToString())
            .Append("&IsSummaryOnly=").Append(args.SummaryOnly.ToString())
            .Append("&CH4RangeCharts=").Append(args.DisplayCH4Charts.ToString());

        string FileName = System.Guid.NewGuid().ToString() + ".pdf";
        string Dir = Server.MapPath("~/reports/");
        string FileFullName = Path.Combine(Dir, FileName);

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_url.ToString());
        req.UseDefaultCredentials = true;
        req.Timeout = Timeout.Infinite;
        req.PreAuthenticate = true;
        req.KeepAlive = true;
        req.Credentials = new System.Net.NetworkCredential(
            ConfigurationManager.AppSettings["SSRS_USERNAME"],
            ConfigurationManager.AppSettings["SSRS_PASSWORD"],
            ConfigurationManager.AppSettings["SSRS_DOMAIN"]);
        try
        {
            using (HttpWebResponse httpResponse = (HttpWebResponse)req.GetResponse())
            {
                using (Stream responseStream = httpResponse.GetResponseStream())
                {
                    using (FileStream localFileStream = new FileStream(FileFullName, FileMode.Create))
                    {
                        int bytesRead;
                        while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            totalBytesRead += bytesRead;
                            localFileStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
         //   MessageBox.Show(e.ToString());
        }
        _Files.Add(0, FileFullName);
    }

这就是我通过 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;
    }

这是我用来合并文档的代码。

有谁知道为什么会发生这个错误以及如何解决这个错误?

感谢所有帮助。

【问题讨论】:

  • 愚蠢的问题,但文件是否肯定存在于目录中?另外,什么是抛出异常?是Path.GetDirectoryName(_Files[0])吗?
  • 创建该文件并向该文档添加 20 页。但该文件不在目录中。

标签: c# asp.net pdf reporting-services ssrs-2008-r2


【解决方案1】:

该文件是否存在于该路径中? 如果是这样,它是不是用另一个用户帐户写的,所以读者没有阅读权限?

【讨论】:

  • 当我收到来自 HTTP 请求的响应时,我在本地创建了这个 pdf 文件。这个文件是自动生成的(你可以在代码中看到)。我的用户帐户有阅读权限。
  • 可能是因为您从未关闭和处置您在 PdfWriter 中使用的文件流吗? PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_CombinedPDFPath, FileMode.Create));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
相关资源
最近更新 更多