Mojolicious :: Lite - 插入图片问题

时间:2011-08-23 15:26:30

标签: perl image mojolicious

当我运行此脚本并打开http://my_server/picture时,我得到的图片为logo - 字符串。

#!/usr/local/bin/perl
use warnings;
use 5.014;
use Mojolicious::Lite;

get '/picture' => sub {
    shift->render();
};

app->start;

__DATA__
@@ picture.html.ep
<!DOCTYPE HTML>
<html>
<body>
<p>Hello</p>
<img src="/absolute/path/TEST.jpg" alt="logo" />
<p>World</p>
</body>
</html>

当我打开这个HTML文件作为普通的HTML文件时,我得到了图片。

使用Mojolicious :: Lite插入图片的正确方法是什么?

2 个答案:

答案 0 :(得分:5)

public目录用于静态数据。只需在脚本旁边创建它并将图像放入。当运行默认设置的开发服务器时,您可以在URL上访问它:

http://localhost:3000/TEST.jpg

因此,您的模板可以包含:

<img src="/TEST.jpg" />

答案 1 :(得分:0)

或者您可以使用:Base64 encoded DATA files

...
__DATA__
@@ picture.html.ep
<!DOCTYPE HTML>
<html>
<body>
<p>Hello</p>
<img src="/absolute/path/TEST.jpg" alt="logo" />
<p>World</p>
</body>
</html>

@@ TEST.jpg (base64)
/9j/4AAQSkZJRgABAQEAZABkAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAD6APoDASIA
...