简体   繁体   English

如何显示图片(img)或pdf(iframe)?

[英]How to display either image (img) or pdf (iframe)?

Dynamic links I display on my page (rest) may result in jpeg, tif or pdf file in the response. 我在页面上显示的动态链接(其余)可能会在响应中生成jpeg,tif或pdf文件。

I tried to do it like this (the images part works fine): 我试图这样做(图像部分工作正常):

fetchContent(uri){
    $.ajax({ 
        type: "GET",            
        url: uri,

        success: function(output, status, xhr) {
            var contentType = xhr.getResponseHeader("Content-Type");

            if(contentType === "image/jpeg" || contentType === "image/tiff"){

                // IMG: get parent div 'container'
                var $container = $('#container', top.frames["content"].document);
                var img = new Image();

                $(img)
                    .load(function(){
                        // initially hide
                        $(this).hide();
                        $(this).attr('id', 'imgId');

                        // parent div:
                        $container
                            // remove previous image
                            .empty()
                            // add this image
                            .append(this);

                        // fade in image effect
                        $(this).fadeIn();

                    })

                    // set the src attribute of the new image to our image
                    .attr('src', uri);

            }else if(contentType === "application/pdf"){
                // PDF: get parent div 'main'
                var $main = $('#main', top.frames["content"].document);

                $main.empty();
                $main.append("<iframe id='contentIFrame' name='contentIFrame' src='" + uri +
                "' width='100%' style='height:100%'></iframe>");
            }
        },
        complete: function(output){
            $('#navigation-block').append(output);
        },

        error: function(output) {
            $('#navigation-block').append(output);
        }
    });
}

The PDF part doesn't work. PDF部分无效。

How do I sort that out? 我该如何解决呢?

BTW, I control both REST server side and jQuery side so I added correct content-type in REST response header. 顺便说一句,我同时控制REST服务器端和jQuery端,因此我在REST响应标头中添加了正确的内容类型。

I checked out PDF.js and would have used it, but my client unfortunately uses IE8. 我签出了PDF.js并会使用它,但是不幸的是我的客户使用IE8。

Thanks 谢谢

This is the final solution to the problem of not knowing in advance what a link is going to return (jpeg, tif or pdf): 这是预先不知道链接将返回什么(jpeg,tif或pdf)的问题的最终解决方案:

function fetchImage(uri, $activeLink){
    // show loader div
    var $loadingStatus = $('#loadingStatus', top.frames["content"].document);
    $loadingStatus.fadeIn();

    // remember (select) the link that has just been activated
    $("#navigation-block>ul>li>a.zactive").removeClass("zactive");
    $($activeLink).addClass('zactive');


    $.ajax({
        type: "GET",            
        url: uri,

        success: function(output, status, xhr) {
            // for images use main parent div 'main-img'
            var $mainImg = $('#main-img', top.frames["content"].document);
            // for pdf use parent div 'main-pdf'
            var $mainPdf = $('#main-pdf', top.frames["content"].document);

            // REST service will always return correct content-type
            var contentType = xhr.getResponseHeader("Content-Type");

            if(contentType === "image/jpeg" || contentType === "image/tiff"){

                // for images we also need child div 'container'
                var $container = $('#container', top.frames["content"].document);
                var img = new Image();

                $(img)
                    .load(function(){
                        // default: hide image
                        $(this).hide();
                        $(this).attr('id', 'imgId');

                        // img parent div:
                        $container
                            // remove previous img 
                            .empty() 
                            // insert this img 
                            .append(this); 

                        // hide pdf div and show img div
                        $($mainPdf).hide();
                        $($mainImg).show();

                        // fade in image 
                        $(this).fadeIn();

                        // remove loader div
                        $loadingStatus.stop(true, true).fadeOut();
                    })

                    // set the src attribute of the new image to this uri
                    .attr('src', uri);

            }else if(contentType === "application/pdf"){

                $mainPdf.empty(); // so I don't have to check whether iframe is already there
                $mainPdf.append("<iframe id='contentIFrame' name='contentIFrame' src='" + uri +
                "' width='100%' style='height:100%'></iframe>");

                // hide img div and show pdf div
                $($mainImg).hide();
                $($mainPdf).show();
            }
        },

        error: function(output) {
            alert("error " + output);
        }
    });
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM