user2341498
user2341498

Reputation: 15

Download pdf files from kendo ui grid

I have a table column called PDF(varbinary). The column will bind in kendo ui grid as hyperlink which download the pdf from the database.

The following is the code that i have so far. Based on the research i have done. Therefore i am implementing a template.

The italic code below showing said error "required )" and i am not quite sure what i am missing here.

columns.Template(@).ClientTemplate("Download file").Title("Download1");

Therefore I would kindly advise to implement the download file (pdf format) in kendo ui grid. Thank you

    @(Html.Kendo().Grid<HH.Models.OrderModel>()

            .Name("OrderGrid")
            .HtmlAttributes(new { @Style = "align:center; font-size:10px; width:600px" })
            .Columns(columns =>
            {


                columns.Bound(p => p.OrderId);
                columns.Bound(p => p.Date).EditorTemplateName("Date").Format("{0:dd/MM/yyyy}");            
                columns.Bound(p => p.CustFullName).Width(120);
                columns.Template(@<text></text>).ClientTemplate("<a href="javascript: void(0);" onclick="DownloadFile();">Download file</a>").Title("Download1");  
                columns.Template(@<text></text>).ClientTemplate("" + Html.ActionLink("<img src='" + Url.Content("~/Content/icons/pdficon_small.png") + "' />", "DocumentDownload2", "Administration", new { id = "#=OrderId#" }, null) + "").Title("Download2");


            })


            .ToolBar(toolbar => toolbar.Save().Text("Save Changes"))
            .Editable(editable => editable.Mode(GridEditMode.InCell))
            .Selectable()
            .Pageable(paging => paging
                .Input(false)
                .Numeric(true)

                .PreviousNext(true)
                .PageSizes(new int[] { 5, 10, 25, 50 })
                .Refresh(false)

            )

            .DataSource(dataSource => dataSource
                .Ajax()//bind with Ajax instead server bind
                .PageSize(10)
                .ServerOperation(true)
                    .Model(model =>
                    {
                        model.Id(p => p.OrderId);

                    })


                           .Read(read => read.Action("GetOrder", "Administration").Type(HttpVerbs.Get))
                            .Update("EditOrder", "Administration")

                )



        )



**controller**
public ActionResult Download1()  
{  
    string contentType = "application/pdf";  
    string filePath = Server.MapPath("~/Files/OrderDetails.pdf");  
    return File(filePath, contentType, "OrderDetails.pdf");  
}  

public ActionResult Download2(int orderId)  
{  
    string contentType = "application/xlsx";  
    string filePath = Server.MapPath("~/Files/OrderDetails.pdf");  
    return File(filePath, contentType, "OrderDetails.pdf_" + orderId.ToString() + ".xlsx");  
} 

Upvotes: 1

Views: 13236

Answers (3)

Andrei Hardau
Andrei Hardau

Reputation: 351

Another way to do the same thing, with ActionLink inside the ClientTemplate:

columns.Template(@<text>
    @Html.ActionLink("get pdf", "getpdf", "administration", new { orderid= @item.OrderId}, null)
    </text>);

As taken from: http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/faq#how-do-i-use-action-links

Upvotes: 0

Atanas Korchev
Atanas Korchev

Reputation: 30671

You can't implement the PDF download on the client-side easily. You should instead stream the PDF file using another action method. You can check this question for some ideas: Retrieve and display image from database using ASP.Net MVC

The grid should contain a link to this action method:

.ClientTemplate("<a href='/administration/getpdf?orderid=#= OrderID #'>get pdf</a>");

public ActionResult GetPdf(int orderID)
{
     // find the pdf by orderid

     return File(stream, "application/pdf", "DownloadName.pdf");
}

Upvotes: 2

Shion
Shion

Reputation: 1499

You'd have to implement this yourself. KendoUI is a client-side technology, and has nothing to do with serving an arbitary PDF from a datasource.

If you'd like to generate a PDF, look up the following resources:

PDF: http://www.kendoui.com/code-library/mvc/grid/export-grid-to-pdf.aspx

Maybe this UserVoice entry: http://feedback.kendoui.com/forums/127393-kendo-ui-feedback/suggestions/3494585-kendoui-mvc-serverside-wrappers-should-allow-expor

Upvotes: 1

Related Questions