2017-06-19 42 views
0

我想在瀏覽器中查看pdf文件,以下代碼在localhost上爲我工作,但不能在dev服務器上工作任何想法這是什麼錯誤。Pdf在本地渲染,但不在開發

此外我想要在網格視圖中點擊鏈接按鈕時在新窗口/選項卡中查看此pdf文件。

protected void lnkBtn_grid_View_Command(object sender, CommandEventArgs e) 
     { 
     try 
     { 
      var fileName = e.CommandArgument.ToString(); 
      var normalTextByte=Encoding.UTF8.GetBytes(fileName); 
      var encryptedFile = MachineKey.Encode(normalTextByte, MachineKeyProtection.All); 
      Response.Redirect("~/statementfilesviewer.aspx?filename=" + Server.UrlEncode(encryptedFile)); 
     } 
     catch (Exception ex) 
     { 
      Error.Log(ex); 
     } 
    } 

    private void ReadPdfFile(string filename) 
    { 
     string keyName = ""; 
     try 
     { 
      string prefix = ConfigurationManager.AppSettings["Environment"] + "/StatementFiles/"; 
      keyName = prefix + SessionFacade.DbId + "/" + _intArtistId + "/" + filename; 

      IAmazonS3 client; 

      using (client = new AmazonS3Client()) 
      { 
       var request = new GetObjectRequest 
       { 
        BucketName = ConfigurationManager.AppSettings["BucketName"], 
        Key = keyName 
       }; 

       string cwrFilePath; 
       using (GetObjectResponse response = client.GetObject(request)) 
       { 
        cwrFilePath = @"~/PDFs/" + filename.Replace(" ", "_").Replace("-", "_"); 

        if (File.Exists(Server.MapPath(cwrFilePath))) 
         File.Delete(Server.MapPath(cwrFilePath)); 

        if (!File.Exists(Server.MapPath(cwrFilePath))) 
        { 
         response.WriteResponseStreamToFile(Server.MapPath(cwrFilePath)); 
        } 
       } 

       if (File.Exists(Server.MapPath(cwrFilePath))) 
       { 
        //Literal1.Text = " <object data=\"/PDFs/" + filename.Replace(" ", "_").Replace("-", "_") + "\" type=\"application/pdf\" height=\"600\" width=\"99%\" style=\"border: none;\"><embed src=\"/PDFs/" + filename.Replace(" ", "_").Replace("-", "_") + "\" type=\"application/pdf\" height=\"600\" width=\"99%\" style=\"border: none;\" /></object>"; 
        Response.Clear(); 
        Response.ContentType = "application/pdf"; 
        Response.AddHeader("Content-Type", "application/pdf"); 
        //Response.WriteFile(cwrFilePath); 
        Response.WriteFile(Server.MapPath(cwrFilePath)); 
        Response.End(); 
       } 
       else 
        Response.Write("File does not exists. Please contact to support team."); 
      } 
     } 
     catch (Exception) 
     { 
      Response.Write("File does not exists. Please contact to support team."); 
      EmailingDal.SendEmail("File does not exists on S3 server.", keyName); 
     } 
    } 
+0

可能是文件路徑錯誤。什麼是你的異常電子郵件顯示keyName? – mjw

+1

*「不能工作」*非常模糊......什麼不正確?你有沒有得到任何錯誤,或者輸出是否與你預期的不符?請儘量提供儘可能多的信息,以便更容易理解您的情況/問題。 – bassfader

+0

沒有錯誤,沒有異常只需導航到statementfilesviewer.aspx而不需要渲染pdf文件 –

回答

0
response.WriteResponseStreamToFile(Server.MapPath(cwrFilePath)); 

這應該被寫入響應對象的文件,該文件是一個客戶端可變。從代碼看來,它是一個在服務器端創建並在客戶端瀏覽器中讀取或顯示的文件。

您可以嘗試使用StreamWriter直接在託管服務器上寫入文件。