简体   繁体   English

内容处置:内联标题不会内嵌显示图像?

[英]Content-disposition:inline header won't show images inline?

I'm trying to show an image inline on a page. 我正试图在页面上显示内嵌图像。 It is being served by a codeigniter controller. 它由一个codeigniter控制器提供服务。

class Asset extends MY_Controller {

    function index( $folder, $file ) {

        $asset = "assets/$folder/$file";

        if ( !file_exists( $asset ) ) {
            show_404();
            return;
        }

        switch ( $folder ) {
        case 'css':
            header('Content-type: text/css');
            break;
        case 'js':
            header('Content-type: text/javascript');
            break;
        case 'images':
            $ext = substr( $file, strrpos($file, '.') );
            switch ( $ext ) {
            case 'jpg':
                $ext = 'jpeg';
                break;
            case 'svg':
                $ext = 'svg+xml';
                break;
            }

            header('Content-Disposition: inline');
            header('Content-type: image/'.$ext);
        }

        readfile( $asset );
    }

}

When I load a image in Chrome of FF its pops up the download window. 当我在Chrome的FF中加载图像时,会弹出下载窗口。 I know when the browser can't display the content inline it will force a download anyway, but these are PNG and GIF images which display in the browser fine otherwise. 我知道当浏览器无法显示内联内容时,无论如何都会强制下载,但这些是PNG和GIF图像,否则会在浏览器中显示。 In IE it doesn't force a download but it displays the image in ASCII. 在IE中它不强制下载,但它以ASCII显示图像。

If I comment out the entire image case it FF and Chrome both display the ASCII but not the image. 如果我注释掉整个图像的情况,FF和Chrome都会显示ASCII而不是图像。

I thought setting the content type would allow FF and Chrome to show the actual image, and also allow the location to be used as an src. 我认为设置内容类型将允许FF和Chrome显示实际图像,并允许该位置用作src。

The javascript and css shows fine of course. javascript和css当然很好。

Anyone got any ideas how I can make the images show properly? 任何人都有任何想法如何使图像正确显示?

Try removing the Content-disposition header. 尝试删除Content-disposition标头。 By default, they should be displayed inline anyway. 默认情况下,它们应该以内联方式显示。

Anyway, you should be doing something wrong. 无论如何,你应该做错事。 I've just tested in Opera and IE and they do indeed display the image inline with the content-disposition header. 我刚刚在Opera和IE中进行了测试,他们确实使用内容处置标题内嵌显示图像。 I used: 我用了:

<?php
header('Content-Disposition: inline');
header('Content-type: image/png');

readfile('Untitled.png');

Use the dev tools of your browser to check the response header. 使用浏览器的开发工具检查响应标头。

EDIT 编辑

Probably this bug is causing the problem: 可能这个错误导致了这个问题:

<?php
echo substr( "file.ext", strrpos("file.ext", '.') );

gives ".ext" not "ext". 给出“.ext”而不是“ext”。 Substitute for strrpos() + 1. 替换strrpos()+ 1。

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

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