简体   繁体   English

使用File://的瓶子显示图像

[英]Bottle Display Image using File://

I am trying to display the image on my local machine. 我正在尝试在本地计算机上显示图像。 I only use the website from my own machine. 我只能从自己的机器上使用该网站。 I am not expecting visit from outside. 我不希望有外界的来访。 I found a solution here: Get Flask to show image not located in the static directory , But it doesn't work for me. 我在这里找到了一个解决方案: 获取Flask以显示不在静态目录中的图像 ,但这对我不起作用。 I have tried: 我努力了:

relative path, abs path. 相对路径,绝对路径。 None of them works. 它们都不起作用。 Where I did it wrong? 我在哪里做错了?

QUESTION: for test purpose, my file system is like this: 问题:出于测试目的,我的文件系统是这样的:

C:/jackson/Python34_workspace/Python34_Projects/Learn-Bottle/app05_rend_local_img/
    picuture_gallery/Desert.jpg
    views/index.tpl
    main.py

python script is this python脚本是这个

@bottle.route("/")
def index():
    return bottle.template("index.tpl")

@bottle.post('/result')
def result():
    return bottle.template("index.tpl")

And this is my template. 这是我的模板。

<form action="/result" method="POST">

<br>
<input type="submit" value="Submit">
<br>
</form>

<div>
    <img src="file:///C:/HSH/Python34_workspace/Python34_Projects/Learn-Bottle/app05_rend_local_img/picture_gallery/Desert.jpg">
</div>

--- Some comment --- I have tried ---一些评论---我已经尝试过

src="file:///picuture_gallery/Desert.jpg"

after I clicked submit, it doesn't display. 单击提交后,它不会显示。 But if I drag it to the browser, it works. 但是,如果我将其拖到浏览器中,它将起作用。 How could that be? 怎么可能

An URL using the file procotol is never requested from the server. 永远不会从服务器请求使用file procotol的URL。 The client (browser) always looks for it on the local system. 客户端(浏览器) 总是在本地系统上寻找它。

So it doesn't matter how you configure your Bottle application, the browser will not ask it for such an URL. 因此,不管您如何配置Bottle应用程序,浏览器都不会向其询问这样的URL。

If you want the Botte application to deliver static files , do something like this: 如果您希望Botte应用程序传递静态文件 ,请执行以下操作:

from bottle import static_file
@route('/static/<filename>')
def server_static(filename):
    return static_file(filename, root='/path/to/your/static/files')

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

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