1、python代码
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)
2、postman请求接口
请求接口时Hearders要设置Content-Type为multipart/form-data 在body中要设置为form-data,然后key类型要改为File类型之后上传图片,请求接口
|