博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]Display PDF within web browser using MVC3
阅读量:6939 次
发布时间:2019-06-27

本文共 5262 字,大约阅读时间需要 17 分钟。

本文转自:

Introduction

I will demonstrate you different way to display PDF within browser using  MVC3

Background 

I have created MVC3 empty project. I have added Controller name Home and created action name Index. I have added Index view for index action. I have added Temp.pdf in my solution.

Using the code

Method 1:- Display PDf by filePath. Let's create action name DispalyPDF in Home controller as fellow and add view for action

public FileResult DisplayPDF(){    return File("/Temp.pdf", "application/pdf"); }

That's it you can able to view PDF in browser.

I have use File function as my return type which return FileResult having overload File(filePaht,contentType).

filePath: Define the path of file. I have file in my root directory name Temp.pdf. I have define my file path as "/Temp.pdf".

contentType: Define the type of file we are returning. If the content type is supported by browser browser will display that file.

I have specified link in the Index view that will navigate to the action DisplyaPDF().

  • @Html.ActionLink("Viw Temp PDF Method1","DisplayPDF")
  • Method 2:- Display PDF by fileContent. Let's create action PDFDispaly() as fellow and add view PDFDisplay

    public FileResult PDFDisplay(){    string filepath = Server.MapPath("/Temp.pdf");    byte[] pdfByte = Helper.GetBytesFromFile(filepath);    return File(pdfByte, "application/pdf"); }

    I have use File function as return type having overload File(fileContent,fileContentType)

    fileContent : define file content as the byte array.

    fileContentType: define the type of file eg pdf, excel, text etc.. 

    I have use server.MapPath method to get actual path of file on server by specifying virtual path as argument. I have created Helper method name GetByetsFromFile that will return fileContent as byte array by accepting actual file path as argument.

    public static byte[] GetBytesFromFile(string fullFilePath)        {            // this method is limited to 2^32 byte files (4.2 GB) FileStream fs = null; try { fs = File.OpenRead(fullFilePath); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, Convert.ToInt32(fs.Length)); return bytes; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } } }

    Method3:- Download PDF File Let's add another link that will provide to download Temp.pdf on ClientSide.

  • @Html.ActionLink("Download Temp PDF","PDFDownload")
  • Let's create action name PDFDownload that will allow user to download PDF.

    public FileResult PDFDownload(){    string filepath =Server.MapPath("/Temp.pdf");    byte[] pdfByte = Helper.GetBytesFromFile(filepath);    return File(pdfByte, "application/pdf", "demoform1"); }

    I have use File function as my return type where overload  accept the fileContent, contentType and fileDownloadName.

    fileDownloadName: we just need to specify the file download name where MVC3 engine will do all the magic for us and download the file on client side with the specified name.

    Method4:- Display PDF File as PartialView. You can not specify the return type File as PartialViewResult. Let's use the HTML 5 tag embed in partialview to display pdf within browser and render the partial view inside div using AJax.ActionLink helper.

    Let's add another actionlink on Index View but this time we will add ajax.actionlink()

  • @Ajax.ActionLink("Viw Temp PDF Method4", "PDFPartialView", new AjaxOptions { UpdateTargetId = "pdfContainer" })
  • Let's create action method PDFPartialView inside Home controller that return partialviewresult as fellow.

    public PartialViewResult PDFPartialView(){    return PartialView();}

    Add view by checking create partial view check box. We have created partial view PDFPartialView. Create embed html 5 tag inside the partial view. Specify src to the relative path of the PDF file as fellow.

    Partial View That display PDF....

    Click on the link "View Temp pdf method3". You will notice pdf loaded inside the div id pdfContainer. 

    Method5:- Display PDF as 64 bit string. This method only work with Chrome browser. We will follow following 3 step process. 

    Step 1 Convert PDF into the 64 bit array.

    Step 2 Convert 64 bit array into 64 bit string. 

    Step3 Display 64 bit string as Src of the embed tag. 

    Note:- Do not forget to specify mime type as type.(e.g. "image/png") 

     Let's create action PDFInCrome in Home controller as follow.

    //only work in the cromepublic PartialViewResult PDFInCrome(){    string filepath = Server.MapPath("/Temp.pdf"); byte[] pdfByte = Helper.GetBytesFromFile(filepath); var strBase64=Convert.ToBase64String(pdfByte); PDFCrome pdfCrome = new PDFCrome(); pdfCrome.Content = string.Format("data:application/pdf;base64,{0}", strBase64); return PartialView(pdfCrome); }

     Let's add partial view PDFInCrome as follow. 

    @model DisplayPDFDemo.Comman.PDFCrome

    We are done with our action and view. Let's call our partial view on click of Ajax.ActionLink in our Index view.

  • @Ajax.ActionLink("Viw Temp PDF Method5", "PDFInCrome", new AjaxOptions { UpdateTargetId = "pdfContainer" })
  •  This is end our simple demo of display PDF within web browser hope you enjoy. 

     

    License

    This article, along with any associated source code and files, is licensed under

     

    转载地址:http://jnfnl.baihongyu.com/

    你可能感兴趣的文章
    .atitit.web 推送实现解决方案集合(3)----dwr3 Reverse Ajax
    查看>>
    Eclipse或SVN—怎样在Eclipse中安装SVNclient插件
    查看>>
    python_不用循环打印1-1000
    查看>>
    docker解决数据存储问题的方案
    查看>>
    [JBoss] - JBAS015874: JBoss AS 7.1.1.Final "Brontes" started - Eclipse中不能正常启动的解决方法...
    查看>>
    logstash启动脚本
    查看>>
    【QT】C++ GUI Qt4 学习笔记3
    查看>>
    nyoj 130 同样的雪花 【哈希】
    查看>>
    HDU 1518 Square
    查看>>
    CentOS 命令【备忘】
    查看>>
    Mac查看端口占用情况
    查看>>
    统计--VARCHAR与NVARCHAR在统计预估上的区别
    查看>>
    SQL Tuning 基础概述05 - Oracle 索引类型及介绍
    查看>>
    Sql Server系列:流程控制语句
    查看>>
    转:windows API 函数大全
    查看>>
    File中操作路径的API(转)
    查看>>
    [AngularJS] Using $anchorScroll
    查看>>
    Centos 6.4 安装erlang&rabbitmq
    查看>>
    C语言的数组初始化
    查看>>
    rtesseract的例子
    查看>>