最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中下载图片文件

Flask crifan 3430浏览 0评论

Flask 下载图片

Flask返回图片 – 简书

flask之文件上传和下载 – 闫耀珍的个人网站

<form id=”form1″ method=”post” action=”/api/upload” enctype=”multipart/form-data”>
    <div>
        <input id=”File1″ type=”file” name=”myfile”/>
        <input type=”submit”>提交</input>
    </div>
</form>

Flask生成下载文件 – 烛影摇红 – 51CTO技术博客

用flask实现一个上传图片到服务器的功能

Python中使用Flask、MongoDB搭建简易图片服务器 – 新客网

python – flask如何返回图片流? – SegmentFault

实际上此处是想要:

在Flask中,已经得到了一个图片的地址了

现在需要去下载图片到服务器的某个路径上

Flask python download image

Flask python download image to static

python – How to return images in flask response? – Stack Overflow

API — Flask Documentation (0.11)

python – How do you serve a dynamically downloaded image to a browser using flask? – Stack Overflow

python – How do i link to images not in Static folder in flask – Stack Overflow

同时还要去搞清楚:

Flask中,如果下载网络上的图片,保存到服务器中

到底直接保存到static中的某个目录好

还是保存到其它什么地方?比如数据库?

flask save image to server

flask download other site image file to server

Uploading Files — Flask Documentation (0.11)

A flask server for serving all JPEG images in a local directory and subdirectories. Uses unveil.js for lazy-loading of images. Thumbnails are created on the fly by PIL.

    {% for image in images %}
        <a class=”image” href=”{{ image.src }}” style=”width: {{ image.width }}px; height: {{ image.height }}px”>
            <img src=”data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==” data-src=”{{ image.src }}?w={{ image.width }}&amp;h={{ image.height }}” width=”{{ image.width }}” height=”{{ image.height }}” />
        </a>
    {% endfor %}

python – Flask Download a File – Stack Overflow

flask image file storage

[AF]Best way to store avatars, images, etc:flask

flask requests download image

python – Using Flask, how can I download a file from a user-given URL? – Stack Overflow

flask app.open_instance_resource

flask app open folder

API — Flask Documentation (0.11)

->

“static_folder

The absolute path to the configured static folder.”

去试试:

app.static_folder

最后是自己去用:

#download avatar image
avatarUrl = respUserInfoDict[‘headimgurl’]
app.logger.debug(“avatarUrl=%s”, avatarUrl)
avatarReq = requests.get(avatarUrl)
app.logger.debug(“avatarReq=%s”, avatarReq)
staticFolder = app.static_folder
app.logger.debug(“staticFolder=%s”, staticFolder)
avatartName = openid + “.png”
app.logger.debug(“avatartName=%s”, avatartName)
avatarStaticPath = os.path.join(“img/avatar”, avatartName)
app.logger.debug(“avatarStaticPath=%s”, avatarStaticPath)
avatarFullPath = os.path.join(staticFolder, avatarStaticPath)
app.logger.debug(“avatarFullPath=%s”, avatarFullPath)
with open(avatarFullPath, ‘wb’) as f:
    f.write(avatarReq.content)
    f.close()
    app.logger.debug(“downloaded avatar=%s ok”, avatarUrl)

而下载了图片。

对应的log:

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:133]:
avatarUrl=http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:135]:
avatarReq=<Response [200]>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:137]:
staticFolder=/root/html/SIPEvents/sipevents/static

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:139]:
avatartName=oswjmv4X0cCXcfkIwjoDfCkeTVVY.png

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:141]:
avatarStaticPath=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:143]:
avatarFullPath=/root/html/SIPEvents/sipevents/static/img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:148]:
downloaded avatar=http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0 ok

转载请注明:在路上 » [已解决]Flask中下载图片文件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.186 seconds, using 22.06MB memory