- 아래의 예제는 ruby는 '2.5.1', rails는 '5.2.3' 버전을 기준으로 작성되었습니다.
- 파일 업로드를 위해 gem에서 제공하는 'carrierwave'라는 업로더를 사용하였습니다.
- 설치 방법 1
- gem install carrierwave
- 설치 방법 2
- Gemfile에 gem 'carrierwave' 입력
- bundle install
- 설치 방법 1
1. 샘플 코드
- 이미지
...
- , 동영상, 파일 업로드 기능과 임포트 기능을 수행하기 위한 MVC를 생성합니다.
- 이미지, 동영상, 파일
...
- 업로드 API: 업로드는 '/upload_file'' API를, 그리고 임포트는 '/import' API를 사용한다는 가정하에 작성되었습니다.
1.1 파일 업로더 생성
| Code Block | ||
|---|---|---|
| ||
> rails generate uploader File |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
class UploadFileController < ApplicationController skip_before_action :verify_authenticity_token # csrf_token def createupload uploaded = UploadFile.create(file: params[:file])... end render json:{def import 'uploadPath': uploaded.file.url }... end end |
1.4 데이터베이스 마이그레이션
| Code Block | ||
|---|---|---|
| ||
# UploadFile에 파일을 담을 'file' 필드 추가 > rails generate migration AddFileToUploadFile file:string > rake db:migrate |
...