網站首頁 編程語言 正文
接下來給搭建講解Flask如何接受圖片文件,上面是復雜寫法,下面是簡單寫法,二選一即可。
思路整理:接收圖片->定義一個圖片存放的位置->給圖片重命名(為了唯一性)->保存操作->返回狀態
方案一:下面是復雜的寫法:
basedir = os.path.abspath(os.path.dirname(__file__)) #定義一個根目錄 用于保存圖片用
import os,sys,random,string
from app import app,request,json,url_for
@app.route('/api/v1/ckeditorUpload',methods=['GET','POST'])
def editorData():
#生成隨機字符串,防止圖片名字重復
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))
#獲取圖片文件 name = upload
img = request.files.get('upload')
#定義一個圖片存放的位置 存放在static下面
path = basedir+"/static/img/"
#圖片名稱 給圖片重命名 為了圖片名稱的唯一性
imgName = ran_str+img.filename
#圖片path和名稱組成圖片的保存路徑
file_path = path+imgName
#保存圖片
img.save(file_path)
#這個是圖片的訪問路徑,需返回前端(可有可無)
url = '/static/img/'+imgName
#返回圖片路徑 到前端
return url
方案二:下面是簡單寫法:
思路:接收圖片 ->定義保存路徑->獲取名稱->保存操作
basedir = os.path.abspath(os.path.dirname(__file__)) #定義一個根目錄 用于保存圖片用
from app import app,request
@app.route('/api/v1/ckeditorUpload',methods=['GET','POST'])
def editorData():
#獲取圖片文件 name = upload
img = request.files.get('upload')
#定義一個圖片存放的位置 存放在static下面
path = basedir+"/static/img/"
#圖片名稱
imgName = img.filename
#圖片path和名稱組成圖片的保存路徑
file_path = path+imgName
#保存圖片
img.save(file_path)
#url是圖片的路徑
url = '/static/img/'+imgName
return url
原文鏈接:https://blog.csdn.net/weixin_38639882/article/details/82344891
相關推薦
- 2023-02-07 詳解C#如何實現窗體換膚_C#教程
- 2022-10-18 AJAX淺析數據交換的實現_AJAX相關
- 2022-02-20 千分位保留兩位小數,出現“toFixed() is not a function”的解決辦法
- 2023-02-07 Go?singleflight使用以及原理_Golang
- 2023-04-03 Python數據結構棧實現進制轉換簡單示例_python
- 2022-05-17 ubuntu解決虛擬機Ubuntu連接不了網絡(親測)
- 2022-06-27 Python使用re模塊實現okenizer(表達式分詞器)_python
- 2022-12-03 如何基于Session實現短信登錄功能_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支