2016-11-11 7 views
2

파이썬 flask_restplus 게시물을 가지고 파일을 가져 와서 푸시하는 방법은 무엇입니까? 서버에 xlsx?Python restplus API를 사용하여 파일을 업로드하고 다운로드합니다.

이 경우 marshaling을 사용해야합니까?

참조 : https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/

이 답변은 일반적인 정보를 줄 수 있지만 파이썬> 플라스크> restplus 컨텍스트에서 : REST API File Upload

+0

단일 게시물 메서드에서 여러 유형/이벤트가 있으면 내 의견을이 모듈 스타일로 유지할 수 없습니다. 보내기 메소드 (파일 업로드)는'post'이지만, 어딘가에서 파일을 쓰기 위해 체크 할 필요가 있기 때문에 결코이 방법을 사용하지 않았습니다! 큰 파일에서 복잡하기 때문에 대상과 동기화 된 소스가 필요합니다 (diff를 만드는 것만 제거하거나 이동하지 마십시오). 일부 데이터 또는 escape_multiple_io 작업을 잃지 않으려면 소스 코드/html 출력에 추가 버전을 사용해야합니다. 작은 크기의 파일이있는 경우 Base64로 인코딩 된 bz2 콘텐츠를 사용하십시오. 큰 파일 (큰 파일 (서버에서))은 프로세스 생성/수정시 많은 위험이 있습니다. – dsgdfg

답변

2

먼저 그런 다음 새로운 추가 파서에게

# 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'} 

이 도움이되기를 바랍니다.

+0

평안하게하기 위해서 먼저 파일 객체를 만들어야한다고 생각합니다 : 'POST/api/v1/file { "메타": 어떤 데이터 "}' 그러면 파일 객체를 받게됩니다 : '{ 'PUT/api/v1/file/1/2 '파일의 바이너리와 함께 PUT을 사용하여 첨부 파일을 추가합니다 : "data", "created_time": "timestamp", "id": 1} 첨부 파일 ' – samurai

+0

간단한 html 양식으로 PUT 또는 POST를 수행하는 경우 – samurai

관련 문제