将图像从文件系统上传到html页面

时间:2021-01-15 22:47:30

I'm trying do develop a very simple web app using flask, following the example from this link.

我正在尝试使用flask开发一个非常简单的Web应用程序,遵循此链接中的示例。

My problem is: I want to show images from my file system in that page but it seems like browsers are protected against that. I have to use something like:

我的问题是:我想在该页面中显示来自我的文件系统的图像,但似乎浏览器受到保护。我必须使用类似的东西:

<img src="http://aMessyURL.png">

Instead of being able to use something like:

而不是能够使用类似的东西:

<img src="images/myImageName.png">

So, I can I (dynamically) show images from my file system?

那么,我可以(动态)显示文件系统中的图像吗?

3 个解决方案

#1


0  

You should be able to do it when hosting a simple server: https://docs.python.org/2/library/simplehttpserver.html

您可以在托管简单服务器时执行此操作:https://docs.python.org/2/library/simplehttpserver.html

This should at least allow you to load files in the same folder as your code.

这至少应该允许您在与代码相同的文件夹中加载文件。

#2


0  

Flask interprets <img src="images/myImageName.png"> as:

Flask将将图像从文件系统上传到html页面解释为:

app/images/myImageName.png

Normally, Flask projects have a static folder inside app (app/static) which contains your CSS, JS and images. You can either render them like above or use url_for:

通常,Flask项目在app(app / static)中有一个静态文件夹,其中包含你的CSS,JS和图像。您可以像上面那样渲染它们或使用url_for:

<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.png') }}">

Which renders fully as:

完全呈现为:

<link rel="shortcut icon" href="/static/img/favicon.png"> 

#3


0  

You Have to specify static root path and then use code in html below

您必须指定静态根路径,然后使用下面的html中的代码

app = Flask(__name__, static_url_path='')

In HTML

<img src="/static/images/myImageName.png">

#1


0  

You should be able to do it when hosting a simple server: https://docs.python.org/2/library/simplehttpserver.html

您可以在托管简单服务器时执行此操作:https://docs.python.org/2/library/simplehttpserver.html

This should at least allow you to load files in the same folder as your code.

这至少应该允许您在与代码相同的文件夹中加载文件。

#2


0  

Flask interprets <img src="images/myImageName.png"> as:

Flask将将图像从文件系统上传到html页面解释为:

app/images/myImageName.png

Normally, Flask projects have a static folder inside app (app/static) which contains your CSS, JS and images. You can either render them like above or use url_for:

通常,Flask项目在app(app / static)中有一个静态文件夹,其中包含你的CSS,JS和图像。您可以像上面那样渲染它们或使用url_for:

<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.png') }}">

Which renders fully as:

完全呈现为:

<link rel="shortcut icon" href="/static/img/favicon.png"> 

#3


0  

You Have to specify static root path and then use code in html below

您必须指定静态根路径,然后使用下面的html中的代码

app = Flask(__name__, static_url_path='')

In HTML

<img src="/static/images/myImageName.png">