用python flask_restplus什麼是正確的方式有一個帖子,並獲得方法來獲取和推送文件例如xlsx到服務器?Python restplus API上傳和下載文件
是否需要使用marshaling?
參考:https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/
這個答案給一般信息,但不是在蟒蛇>瓶> restplus方面:REST API File Upload
用python flask_restplus什麼是正確的方式有一個帖子,並獲得方法來獲取和推送文件例如xlsx到服務器?Python restplus API上傳和下載文件
是否需要使用marshaling?
參考:https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/
這個答案給一般信息,但不是在蟒蛇>瓶> restplus方面:REST API File Upload
首先,你需要配置一個解析器
# parsers.py
import werkzeug
from flask_restplus import reqparse
file_upload = reqparse.RequestParser()
file_upload.add_argument('xls_file',
type=werkzeug.datastructures.FileStorage,
location='files',
required=True,
help='XLS file')
然後添加一個新的資源給你的api命名空間
# api.py
import …
import parsers
@api.route('/upload/')
class my_file_upload(Resource):
@api.expect(parsers.file_upload)
def post(self):
args = parsers.file_upload.parse_args()
if args['xls_file'].mimetype == 'application/xls':
destination = os.path.join(current_app.config.get('DATA_FOLDER'), 'medias/')
if not os.path.exists(destination):
os.makedirs(destination)
xls_file = '%s%s' % (destination, 'custom_file_name.xls')
args['xls_file'].save(xls_file)
else:
abort(404)
return {'status': 'Done'}
我希望這有助於。
如果在單個帖子方法中獲得多個類型/事件,我的意見不能以此模塊樣式成功。發送方法(文件上傳)是'post',但我從來沒有使用過這種方法,因爲需要檢查文件內容以便寫入某處!在大文件上很複雜,需要與目標同步源代碼(不能刪除或只移動diff)。有時需要在源代碼/ html-output上使用額外的修訂版,以免丟失任何數據或escape_multiple_io操作。如果使用小尺寸文件,請使用Base64編碼的bz2內容。大文件(這很大?(在你的服務器上))在創建/修改過程中遇到很多風險。 – dsgdfg