2017-06-13 77 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