Как отобразить .pdf файл в модальном окне? - PullRequest
0 голосов
/ 03 мая 2018

У меня есть .pdf и .html файл на сервере. Я хочу отобразить файл .pdf в модальном окне. В настоящее время мой .html файл работает нормально.

@item.ExampleUrl дает мне .pdf и .html файл.

Подскажите пожалуйста, как заполнить .pdf в модале.

Пожалуйста, посмотрите файл .pdf на скриншоте

enter image description here

Сценарий

 $(document).ready(function () {
            $('.openExamplefile').on('click', function () {
                debugger;
                var url = $(this).attr("data-url"); //page url  
                if (url == "/files/examples/" || url == "/files/examples/ ") {
                    alert("There is no html file exists!")
                }
                else {
                    $("#PedigreesSireRacingModal").load(url, function () {
                      $("#PedigreesSireRacing").modal({ show: true });
                    });
                }

            });

        });

Контроллер

public FileResult Download(string FileName)
{           
            return File("~/files/examples/" + FileName, MimeMapping.GetMimeMapping(FileName),FileName);
}

Мой модал

<button id="ButtontoLink" type="button" class="openExamplefile" 
     data-url="/files/examples/@item.ExampleUrl">Example</button>

<div class="modal" id="PedigreesSireRacing" tabindex="-1" role="dialog">
    <div class="modal-dialog" style="width:1250px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><label id="ProductName"></label>  </h4>
            </div>
            <div id="PedigreesSireRacingModal" class="modal-body racing">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Ответы [ 3 ]

0 голосов
/ 03 мая 2018

Вы пытались вставить PDF в модал? Как это:

<embed src="pdf-example.pdf" frameborder="0" width="100%" height="400px">
0 голосов
/ 03 мая 2018

Попробуйте использовать FileStreamResult в качестве типа возврата вместо FileResult:

public ActionResult Download(string fileName)
{
    var stream = new FileStream(Server.MapPath("~/files/examples/" + fileName), FileMode.Open, FileAccess.Read);

    var result = new FileStreamResult(stream, "application/pdf");

    return result;
}

Затем поместите тег <embed> внутри модального <div> элемента в качестве заполнителя PDF:

<div id="PedigreesSireRacingModal" class="modal-body racing">
    <embed src="@Url.Action("Download", "ControllerName", new { fileName = "FileName.pdf" })" width="100%" height="100%" type="application/pdf">
    </embed>
</div>

Обновление 1:

Поскольку вы можете вернуть формат HTML или PDF, установите флажок для расширения файла и используйте правильный тип содержимого в контроллере и теге embed / object:

Контроллер

public ActionResult Download(string fileName)
{
    string path = Server.MapPath("~/files/examples/" + fileName);
    var stream = new FileStream(path, FileMode.Open, FileAccess.Read);

    string contentType = MimeMapping.GetMimeMapping(path);

    var result = new FileStreamResult(stream, contentType);

    return result;
}

View

<!-- example if using viewmodel property -->
<object data="@Url.Action("Download", "ControllerName", new { fileName = Model.FileName })" width="100%" height="100%">
</object>

<embed src="@Url.Action("Download", "ControllerName", new { fileName = Model.FileName })" width="100%" height="100%" type="@Model.ContentType">
</embed>

В качестве примечания попробуйте использовать Content-Disposition параметр заголовка ответа в методе действия контроллера, чтобы убедиться, что тип файла установлен правильно:

var contentDisposition = new System.Net.Mime.ContentDisposition
{
    FileName = fileName,
    Inline = true
};

Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
0 голосов
/ 03 мая 2018

Вы можете использовать тег <embed> внутри вашего мода, например,

<button id="ButtontoLink" type="button" class="openExamplefile" 
     data-url="/files/examples/@item.ExampleUrl">Example</button>

<div class="modal" id="PedigreesSireRacing" tabindex="-1" role="dialog">
    <div class="modal-dialog" style="width:1250px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><label id="ProductName"></label></h4>
            </div>
            <div id="PedigreesSireRacingModal" class="modal-body racing">
                <embed src="/path/to/pdf_file.pdf" type="application/pdf" width="100%" height="100%">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...