Ad

Search This Blog

Friday, May 22, 2015

How to open a PDf document in Next Window of Browser when the Anchor tag clicked in Web grid in MVC4 with C#

Web Grid Code:

 <div id="">
        <table >
            <tr>
                <td align="left" colspan="2" style="vertical-align:top;">
                    @grid.GetHtml(tableStyle: "webgrid-table",
                        headerStyle: "webgrid",
                        footerStyle: "webgrid-footer",
                        alternatingRowStyle: "webgrid-alternating-row",
                        rowStyle: "webgrid-row-style",
                    columns:
                     grid.Columns
                        (
                            grid.Column("CompanyName", "Company Name"),
                            grid.Column("FileName", "FileName "),
                            grid.Column("FileUploadDate", "File Uploaded Date"),
                            grid.Column(format: @<a href="~/SupplierDocuments/BindImageData/@item.FileUploadId"  style="color:blue;" >&nbsp;Download&nbsp;</a>),
                            grid.Column(format: @<a href="~/SupplierDocuments/ViewPDFDocument/@item.FileUploadId" target="_blank" style="color:blue;">&nbsp;View&nbsp;   </a>)
                        ))
                 </td>
            </tr>
         
        </table>
      
    </div>


C# Code : 

Binding the WegGrid Code:

 public ActionResult SupplierDocuments()
        {
            var fileContentViewDetails = new List<FileContentViewDetails>();
            var sRepository = new SRepository();
            try
            {
                var userId = Convert.ToInt32(Session["UserId"].ToString().Trim());
                fileContentViewDetails = sRepository.GetSuppliersDocumentData(userId).ToList();
            }
            catch (Exception ex)
            {

            }
            return View(fileContentViewDetails.ToList());
        }





 public ActionResult ViewPDFDocument(int? Id)
        {
            var filePath = Server.MapPath("~/Supplier.Web/images/");
            var sRepository = new SRepository();
            var downloadFileName = sRepository.GetDownloadFileName(Id);
            byte[] imageByteData = sRepository.GetPdfFileContent(Id);
            string imageBase64Data = Convert.ToBase64String(imageByteData, 0, imageByteData.Length);
            MemoryStream workStream = new MemoryStream();
            workStream.Write(imageByteData, 0, imageByteData.Length);
            workStream.Position = 0;
            Response.AppendHeader("Content-Disposition", "inline; filename=" + downloadFileName.ToString().Trim());
            Response.ContentType = "application/pdf";
            return File(workStream, "application/pdf", downloadFileName.ToString().Trim());
        }

No comments:

Post a Comment