import os
from flask import Flask, request
app = Flask(__name__)
from flask import jsonify
from werkzeug.utils import secure_filename
# 上传的图片保存路径
UPLOAD_PATH = os.path.join(os.path.dirname(__file__), 'images')
@app.route('/api/upload', methods=['POST'])
def upload_pic():
# 来获取多个上传文件
imgs = request.files.getlist("file_imgs")
urls = []
# 上传文件夹如果不存在则创建
if not os.path.exists(UPLOAD_PATH):
os.mkdir(UPLOAD_PATH)
# 循环读取上传的文件并保存
for img in imgs:
filename = secure_filename(img.filename)
print(filename)
img.save(os.path.join(UPLOAD_PATH, filename))
msg = "http://127.0.0.1:5006/images/{}".format(filename)
urls.append(msg)
respose = {
"code": 200,
"urls": urls
}
return jsonify(respose)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5006, debug=False)
请求接口时Hearders要设置Content-Type为multipart/form-data
在body中要设置为form-data,然后key类型要改为File类型之后上传图片,请求接口