2017-06-13 78 views
0

我有一个网站从报告服务器生成报告。我需要通过URL将参数传递给报表服务器。收到错误“〜 filename.pdf找不到文件或资源。”

Response from for HTTP request ,这是我得到的回应。之后,我创建了一个本地文件流来存储响应中的数据。但问题是当我尝试访问本地文件与其他文件合并它返回错误

“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; 
    } 

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

被人知道为什么这个错误是发生以及如何解决这个问题?

所有帮助表示赞赏。

+0

愚蠢的问题,但存在一定的目录中的文件吗?另外,什么是抛出异常?它是否是'Path.GetDirectoryName(_Files [0])'? – Poosh

+0

该文件已创建,并将20页添加到文档。但该文件不在目录中。 –

回答

0

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

+0

我在本地创建了这个pdf文件,当我从HTTP请求得到响应时。这个文件是自动生成的(U可以在代码中看到它)。我的用户帐户有权读取。 –

+0

是否可能是由于您从未关闭并处理您在PdfWriter中使用的文件流而导致的? 'PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream(_CombinedPDFPath,FileMode.Create));' – Henrik