2017-06-19 2 views
0

ブラウザでpdfファイルを表示したいのですが、次のコードはlocalhostで動作しますが、devサーバでは動作しません。Pdfはローカルではなくdevにレンダリングする

また、この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

エラーも例外もなく、pdfファイルをレンダリングせずにstatementfilesviewer.aspxに移動するだけです –

答えて

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

これは、クライアント側の変数である応答オブジェクトにファイルを書き込みする必要があります。コードからは、サーバー側で作成されたファイルであり、クライアントブラウザーで読み取ったり表示したりします。

StreamWriterを使用して、直接ホスティングサーバーにファイルを書き込むことができます。

関連する問題