admin管理员组

文章数量:1318564

i was thinking of opening a pdf file in modal . current using

@Html.ActionLink("open file", "Download")

open another windows. i want it to populate it to a div so that i can view in in modal or overlay.

i am ok with jquery solution as well as currently my jquery return me some bytes character.

function openfile() {
    $.ajax({
        url: '@Url.Action("Download", "mycontroller")',
        type: "POST",
        dataType: "application/pdf",
        success: function (data) {
            $('#showpdf').html(data);
        },
        error: function (err) {
           alert(err)
        }
    });
}

Action in the controller:

public ActionResult Download()
{
    string path = HttpContext.Server.MapPath("/pdf/service_reports/SR26175.pdf");
    return File(path, "application/pdf");
}

i was thinking of opening a pdf file in modal . current using

@Html.ActionLink("open file", "Download")

open another windows. i want it to populate it to a div so that i can view in in modal or overlay.

i am ok with jquery solution as well as currently my jquery return me some bytes character.

function openfile() {
    $.ajax({
        url: '@Url.Action("Download", "mycontroller")',
        type: "POST",
        dataType: "application/pdf",
        success: function (data) {
            $('#showpdf').html(data);
        },
        error: function (err) {
           alert(err)
        }
    });
}

Action in the controller:

public ActionResult Download()
{
    string path = HttpContext.Server.MapPath("/pdf/service_reports/SR26175.pdf");
    return File(path, "application/pdf");
}
Share Improve this question edited Apr 19, 2017 at 7:43 MVC newbie asked Apr 19, 2017 at 6:22 MVC newbieMVC newbie 5993 gold badges11 silver badges30 bronze badges 5
  • It's not possible into div, only another window or image – user4074041 Commented Apr 19, 2017 at 6:30
  • so can i put a <img> and put pdf inside a image and put that image in div? – MVC newbie Commented Apr 19, 2017 at 6:34
  • lol - nope. You can transform your PDF into IMG, and IMG into base64 data format, and then show inside <img>. – user4074041 Commented Apr 19, 2017 at 6:36
  • You can make use of iframe for this purpose. – mmushtaq Commented Apr 19, 2017 at 6:39
  • @anete.anetes lol. i do not know that haha. – MVC newbie Commented Apr 19, 2017 at 7:19
Add a ment  | 

3 Answers 3

Reset to default 4

Another way to do this is pass pdf path from json request and then use object inside the modal div:

<object id="myPdf" type="application/pdf" data="path/file.pdf"></object>

function openfile() {
    $.ajax({
        url: '@Url.Action("Download", "mycontroller")',
        type: "POST",
        dataType: "application/json",
        success: function (data) {
            $('#showpdf').html('<object id="myPdf" type="application/pdf" data="' + data.path +'"></object>');
        },
        error: function (err) {
            alert(err)
        }
    });
}

public ActionResult Download()
{
    string path = HttpContext.Server.MapPath("/pdf/service_reports/SR26175.pdf");
    return Json(new {path = path});
}

EDIT: For better user experience on different browsers that maybe doesn't support object tag, we could use object with embed tag as a fallback:

<object data="/path/file.pdf" type="application/pdf" width="700px" height="700px">
    <embed src="/path/file.pdf">
        This browser does not support PDFs. Please download the PDF to view it: <a href="/path/file.pdf">Download PDF</a>.</p>
    </embed>
</object> 

Assuming you have a controller named home. You can use an iframe like this

<div id='yourModel'>
   <iframe src="/Home/Download"></iframe>
</div>

and use jQuery to show / hide the div.

I've trying this but at the end I've made this becacuse I need to show diferent pdfs, that are stored (just the full path) in a DB and on a shared folder, then I have to show it one by one to the user, well hope it works for you!

on the view inner the modal body.

<div class="row" style="text-align: center;">
                <object id="pdfViewer" data="~/Evidence/PdfFile.pdf" type="application/pdf" width="790" height="500"></object>
                <div style="text-align: center;">
                    <label id="errorTxt"></label>
                </div>
            </div>

on the controller in a jsonresult.

            //HERE I GET THE REAL PATH OF THE FILE
            string evidence = _manager.GetEvidence(id);

            //CREATING THE NEW PATH FOR THE FILE
            var path = Path.Combine(Server.MapPath("~/Evidence/"), "PdfFile.pdf");

            //SAVE TEMPORARILY ON EVIDENCE FOLDER INNER THE PROYECT
            //WITH THE NEW NAME 
            System.IO.File.Copy(evidence, path, true);

            //AND JUST RETURN A JSON WITH A MESSAGE TO OPEN MODAL
            return Json("OK",JsonRequestBehavior.AllowGet);

and the js function with jquery

function ShowExistEvidence(id) {
    $.ajax({
        url: "@Url.Action("GetEvidence","ControllerName")",
        type: "POST",
        data: {
            id: id
        },
        success: function (data) {
            if (data === 'OK') {                    
                $("#modViewEvidencia").modal("show");
            } else {
               $("#errorTxt").text('File not found');
               $("#modViewEvidencia").modal("show");
            }

        }
    });
}

本文标签: javascriptopening a pdf file in modal possibleStack Overflow