2017-10-10 7 views
0

MVCでPDFにvar-binaryデータを表示する方法。 MVCでPDFとしてvar-binaryデータを表示する方法を誰とでも共有できますか?MVCでvar-binaryデータをPDFに表示するにはどうすればよいですか?

ここではMVCを試みましたが、PDFを表示しませんでした。

MVCコード:ここ

[HttpPost] 
    public ActionResult ViewPDF() 
    { 
     string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">"; 
     embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>"; 
     embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file."; 
     embed += "</object>"; 
     TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/1.pdf")); 

     return RedirectToAction("Index"); 
    } 

は、物理的なパスを呼び出しているが、私は読んで表示VAR-バイナリそうすることができます誰でもシェアのアイデア、

もう一つ私する必要がありますか?。 asp.netアプリケーションでPDFにvar-binaryを表示しましたが、MVCに表示できません。

> Asp.net code samples:- 
window.open('http://localhost:58158/AspForms/pdf.aspx' + '?id=' + id, '', 'width=800, height=650, top=0, left=250, status=0,toolbar=0'); 
> 

PDFポップアップページ:

protected void Page_Load(object sender, EventArgs e) 
     { 
      string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"800px\" height=\"550px\">"; 
      embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>"; 
      embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file."; 
      embed += "</object>"; 

      ltEmbed.Text = string.Format(embed, ResolveUrl("~/FileCS.ashx?Id="), Request.QueryString["id"]); 
     } 

FileCS.ashx: -

<%@ WebHandler Language="C#" Class="FileCS" %> 

using System; 
using System.Web; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
public class FileCS : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     #region 
     int id = int.Parse(context.Request.QueryString["Id"]); 
     byte[] bytes = { }; 
     string fileName = "", allow = "N"; 
     string constr = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(constr)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.CommandText = "SELECT Scan_Pdf_File FROM PWF_InvoiceMain WHERE [email protected] and Enabled = 1"; 
       cmd.Parameters.AddWithValue("@Id", id); 
       cmd.Connection = con; 
       con.Open(); 
       using (SqlDataReader sdr = cmd.ExecuteReader()) 
       { 
        if (sdr.HasRows == true) 
        { 
         sdr.Read(); 
         bytes = (byte[])sdr["PDFFile"]; 
         fileName = "Report"; 
         allow = "A"; 
        } 
       } 
       con.Close(); 
      } 
     } 

     if (allow == "A") 
     {    
      context.Response.Buffer = true; 
      context.Response.Charset = ""; 
      if (context.Request.QueryString["download"] == "1") 
      { 
       context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
      } 
      context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      context.Response.ContentType = "application/pdf"; 
      context.Response.BinaryWrite(bytes); 
      context.Response.Flush(); 
      context.Response.End();    
     } 
     else 
     { 

     } 
     #endregion 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

が、PDFにVAR-バイナリを表示することができませんでしMVCで...

+0

は、データベースやファイルシステムから取得したPDFデータですか?あなたのニーズに応じて、戻り値の型を 'FileStreamResult'または' FileResult'として使うことができると思います。https://stackoverflow.com/questions/30781996/extract-pdf-from-database-and-display-it-on-my- cshtmlを参照してください。 –

+0

それはDBからのvar-binaryデータなので、var-binaryデータを上記のようににバインドする方法は、Page_Loadで述べたようにです。それをLiteralにプッシュすると、fileCS.ashxファイルが呼び出されます - > SELECT Scan_Pdf_FileからPWF_InvoiceMainどこInvoiceID = @ IdとEnabled = 1それからバイトにストリームに変換、asp.netアプリケーション上でうまくいきました。 MVCのみのトラブル – ethiraj

答えて

0

ポップアップv IEW:スキャンコントローラ上の

@using (Html.BeginForm("DisplayPDF", "Scan", FormMethod.Post)) 
    { 
     <a href="javascript:;" onclick="document.forms[0].submit();">View PDF</a> 
    } 

: -

public ActionResult DisplayPDF() 
     { 
      byte[] byteArray = GetPdfFromDB(4); 
      MemoryStream pdfStream = new MemoryStream(); 
      pdfStream.Write(byteArray, 0, byteArray.Length); 
      pdfStream.Position = 0; 
      return new FileStreamResult(pdfStream, "application/pdf"); 
     } 

     private byte[] GetPdfFromDB(int id) 
     { 
      #region 
      byte[] bytes = { }; 
      string constr = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; 
      using (SqlConnection con = new SqlConnection(constr)) 
      { 
       using (SqlCommand cmd = new SqlCommand()) 
       { 
        cmd.CommandText = "SELECT Scan_Pdf_File FROM PWF_InvoiceMain WHERE [email protected] and Enabled = 1"; 
        cmd.Parameters.AddWithValue("@Id", id); 
        cmd.Connection = con; 
        con.Open(); 
        using (SqlDataReader sdr = cmd.ExecuteReader()) 
        { 
         if (sdr.HasRows == true) 
         { 
          sdr.Read(); 
          bytes = (byte[])sdr["Scan_Pdf_File"]; 
         } 
        } 
        con.Close(); 
       } 
      } 

      return bytes; 
      #endregion 
     } 
関連する問題