使用Kendo Grid的MVC中的PDF

时间:2014-12-01 06:11:41

标签: asp.net-mvc pdf kendo-ui kendo-grid

我正在使用Kendo网格。在该网格中,第一列是超链接列。如果我单击每个超链接,则应将超链接的选定文本传递给控制器​​操作。该超链接文本实际上是pdf文件名。因此,如果在网格上单击文件名,则应在标记中打开相同的文件是同一页面本身。用于查看PDf的网格和标签都在同一页面中。怎么做?

Kendo grid://这个网格将在Home.cshtml中,我也把它分成两个。一个将使该网格向左浮动而另一个将向右浮动,如下所示

<object data=@url.action("GetPdf")></object>

@(Html.Kendo().Grid<Cutomers.Model.CustomerDataModel>()
        .Name("grid")
        .Columns(columns =>
        {
          columns.Bound(p => p.FileName).ClientTemplate( "<a href='" + Url.Action("ProductDetails", "Product") + "/#= FileName #'" + ">/#= FileName #</a>" ); 
        columns.Bound(c => c.CreatedDate).Width(70);
        columns.Bound(c => c.CreatedBy).Width(70);
              })

    .HtmlAttributes(new { style = "height: 350px;" })
    .Scrollable()
    .Groupable()
    .Sortable()
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(1))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Customers_Read", "Home"))
    )
)

我的控制器:

public FileStreamResult GetPDF()
        {
            FileStream fs = new FileStream("D:/pdfSample.pdf", FileMode.Open, FileAccess.Read);
            return File(fs, "application/pdf");
        }

1 个答案:

答案 0 :(得分:0)

********网格********************* ******

 @(Html.Kendo().Grid<SearchEmailHistoryGridModel>()
            .Name("EmailSearchGrid")
            .Columns(columns =>
            {

                columns.Bound(p => p.ExtractId).Visible(false);
                columns.Bound(p => p.FileName).Title("File Name");
                columns.Bound(p => p.TemplateName).Title("Template");
                columns.Bound(p => p.CentreName).Title("Centre");
                columns.Bound(p => p.LeaseId).Title("Lease ID");
                columns.Bound(p => p.Unit).Title("Unit").Width("80px");
                columns.Bound(p => p.Occupant).Title("Occupant");
                columns.Bound(p => p.ContactName).Title("Contact");
                columns.Bound(p => p.Attachment).Title("Attachment").ClientTemplate("# if(data.Attachment!=null) {# <div style='cursor: hand;'><a class='gridLink' href='\\#' id='slpitLink'  title='Download #:Attachment#' onclick=\"DownloadFile(#:ExtractId#,'#:FileName#','#:Attachment#'); return false;\">View</a></div> #}#");
                columns.Bound(p => p.EmailAddress).Title("Email").Width("150px");
                columns.Bound(p => p.EmailSent).Title("Email Sent").Visible(false);
                columns.Bound(p => p.FirstSentDate).Title("Date Sent").Width("85px");
                columns.Bound(p => p.SentBy).Title("Sent By").Width("85px");
                columns.Bound(p => p.Delivered).Title("Delivered").Width("85px");
                columns.Bound(p => p.Read).Title("Read").Width("70px");
                columns.Bound(p => p.Exception).Title("Error").ClientTemplate("# if(data.Exception=='Yes') {# <a style='color:\\#94BB27; text-decoration:none; cursor:help' title='#:data.ExceptionDescription#'>Yes</a> #} else {# <label>No</label>  #}#").Width("60px");



                columns.Command(command =>
                {
                    command.Custom("Resend").Click("ResendEmail").HtmlAttributes(new { title = "Resend Email" });
                    command.Custom("Forward").Click("SendForwardEmail").HtmlAttributes(new { title = "Forward Email" });
                }).Title("Action");


            })
            .Events(e => e.DataBound("onEmailSearchDataBound"))
            .Pageable(pageable => pageable
                    .Refresh(true)
                    .PageSizes(true)
                    .PageSizes(new int[] { 5, 10, 20,50 })
                    .ButtonCount(5)
                    )
            .Filterable()                
            .Scrollable()
            .HtmlAttributes(new { style = "height:650px;" })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Events(events => events.Error("error_handler"))  
                .Model(model =>
                    {
                        model.Id(c => c.ExtractId);

                    })   
                .Read(read => read.Action("ReadEmailHistory", "EmailManagement").Data("GetSearchFilterData")) 

            )

        )

**********脚本*************** ********************

  //Download File
          function DownloadFile(extractId,folder,fileNameParam) {


              $.post('@Url.Action("DownloadFile", "Correspondence")', { extractId: extractId, folder: folder, fileName: fileNameParam })
                .done(function (data) {
                    if (data.url == null) {

                        showMessage(data);
                    }
                    else {

                        $("body").append("<iframe src='" + data.url + "' style='display: none;'></iframe>");
                    }

                });

          }

**********************************控制器************ ****************

   //Download File
    [HttpPost]
    [FilterSessionExpire]
    public ActionResult DownloadFile(int extractId,string folder, string fileName)
    {

        var filePath = Path.Combine(ConfigurationManager.AppSettings["EcorsSplitFilePath"],folder, fileName); //in folder level

        if (System.IO.File.Exists(filePath))
        {
            //return file link
            return Json(new { url = Url.Action("DownloadFileActual", "Correspondence", new { folder = folder, fileName = fileName }) }, JsonRequestBehavior.AllowGet);
        }

        //else return error
        return Json("File:" + fileName + " Not found.", JsonRequestBehavior.AllowGet);
    }


   //Actual Download File 
    public ActionResult DownloadFileActual(string folder, string fileName)
    {
        const string contentType = "application/pdf";
        var filePath = Path.Combine(ConfigurationManager.AppSettings["EcorsSplitFilePath"], folder, fileName); //in folder level
        return File(filePath, contentType, fileName);


    }