文件目录
├── main
│ ├── __init__.py
│ ├── demoapi.py
│ ├── errors.py
│ └── tests
│ ├── __init__.py
│ └── test_upload_files.py
接收文件接口demoapi.py
import flask_restful
from flask_restful import request
from werkzeug.datastructures import FileStorage
from flask import Flask
from flask_restful import Resource, Api, reqparse
app = Flask(__name__)
api = Api(app=app)
#
# DataApi
#
class DataApi(Resource):
"""
数据接口
"""
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('file', required=True, type=FileStorage, location='files')
def post(self):
file = request.files['file']
# print(file.name, file.mimetype, file.stream)
file.save('test.png')
return 'OK'
api.add_resource(DataApi, '/data')
if __name__ == '__main__':
app.run(debug=True)
测试代码:test_upload_file.py
# coding:utf-8
# Created by Allen Zhang
# Date: 2018/3/26
import requests
def test_uploads():
file = '/Users/zhang/Desktop/test.png'
url = "http://127.0.0.1:5000/data"
files = {'file':open(file,'rb')}
# print(files)
response = requests.request("POST", url, files=files)
print(response.text)
结果: