fb_license

技術標籤

@selector (1) 初使化區塊 (1) 物件 (1) 物件導向 (2) 型別 (4) 封裝 (1) 流程控制 (1) 陣列 (3) 推論型別 (2) 實機測試 (1) 蓋索林(gasolin) (1) 模組 (1) 憑證 (1) 轉型 (1) 羅康鴻 (121) 類別 (1) 變數 (5) Accelerometer (1) ActiveRecord (1) Activity (1) AFNetworking (1) alloc (1) Android (3) Animation (1) App (1) App ID (1) APP上傳 (1) ASP.NET (1) AVAudioPlayer (1) block (1) C# (2) class (1) CLLocationManager (1) CLLocationManagerDelegate (1) CMMotionManager (4) Controller (1) delegate (1) DELETE語法 (1) Device Motion資料 (1) Dialog (1) DropDownList (1) dynamic language (2) Facebook SDK (9) FBRequest (5) FBRequestConnection (2) FMDB (1) Gesture Recognizers (6) GROUP BY (2) Gyro (1) HAVING (1) IBAction (1) IBOutlet (1) id (3) inheritance (1) init (1) Insert (1) instance variable (1) Interface Builder (1) iOS (70) iOS idea (7) iOS Introduction (1) Layout (1) Magnetometer (1) Menu (2) Method (2) MKMapView (1) MKPointAnnotation (1) MS SQL (5) Nil (1) NSArray (1) NSDictionary (1) NSError (1) NSFileManager & .plist (1) NSMutableArray (1) NSMutableDictionary (1) NSNotificationCenter (1) NULL (1) object (2) Objective-C (16) Objective-C idea (1) ORDER BY (1) Parameter (1) property (1) protocol (2) Provisioning (1) Proximate Sensor (1) Q and A (2) R類別 (1) Rails (9) RESTful SOA (9) Ruby (8) Scene (1) SEELECT (1) Segue (2) SEL (1) SELECT語法 (1) Shake (1) Simulator (1) SOA (8) SQL (6) SQL Server (5) SQL函數 (1) SQL彙總函數 SQL (1) SQLite (1) Storyboard (1) Style (1) Swift (1) Table (1) target & action (1) Theme (1) Toast (1) TRUNCATE TABLE語法 (1) UIActionSheet (1) UIActionSheetDelegate (1) UIActivityIndicatorView (1) UIAlertView (1) UIBarButtonItem (1) UIButton (1) UICollectionView (1) UICollectionViewDataSource (1) UIControl (9) UIDatePicker (1) UIImage (1) UIImagePickerController (2) UIImagePickerControllerDelegate (2) UIImageView (1) UILabel (1) UILongPressGestureRecognizer (1) UINavigationController (2) UIPanGestureRecognizer (1) UIPinchGestureRecognizer (1) UIProgressView (1) UIResponder (1) UIRotationGestureRecognizer (1) UISegmentedControl (1) UISlider (1) UIStepper (1) UISwipeGestureRecognizer (1) UISwitch (1) UITabBarController (1) UITableView (1) UITableViewDataSource (1) UITapGestureRecognizer (1) UITextField (1) UITextFieldDelegate (1) UITextView (2) UITextViewDelegate (1) UIToolBar (1) UIView (8) UIWebView (1) UPDATE語法 (1) var (2) VB.NET (7) View (4) WHERE子句 (1) XML (1)

2013/11/04

[Rails] RESTful SOA Part V - Upload & Download File


我要上傳檔案與下載檔案?!
 OK,交給File類別、ActionController::UploadedFile模組與send_file()方法吧!




1. 建立RESTful SOA Service - rails new
  • 開啟終端機
  • 輸入rails new attachment_service,建立名為attachment_service的SOA服務




2. routes.rb 設定"Upload"&"Download"URI
  • 於\config找到routes.rb
  • 接下來我們將透過"post"和"get"方法宣告上傳與下載的URI並對應到Controller,讓Controller對資源的請求進行回應,如下:
AttachmentService::Application.routes.draw do #上傳URI post 'attachments' => 'attachments#upload', :defaults => {:format => "json"} #下載URI get 'attachments/:id' => 'attachments#download' end

3. Model儲存上傳的檔案資訊 - rails generate model

  • 開啟終端機,並切換到attachment_service目錄下
  • 輸入"rails generate model attachment original_filename:string filename:string"指令並執行,建立名為attachment的Model,目的是用來保存上傳檔案的資訊,其中original_filename保存原始的檔名,而filename則是保存實際儲存的檔名。
  • 輸入"rake db:migrate"指令並執行,實際建立attachments資料表

4. Controller處理URI請求 - rails generate controller
  • 開啟終端機,並切換到attachment_service目錄下
  • 敲入"rails generate controller attachments"指令並執行,建立名為attachments的Controller
  • 於attachment_service\app\controllers,開啟attachments_controller.rb
  • 加入upload方法,其會透過file參數取出上傳的檔案,而後透過File類別實際儲存資料到file目錄下,檔案的資訊在透過Attachment model存在資料庫中,其中的original_filename欄位保存原始上傳的檔案名稱,filename欄位保存實際儲存在server上的檔案名稱
  • 加入download方法,透過id取得Attachment物件,找出檔案的資訊,在透過send_file()方法將檔案傳回
class AttachmentsController < ApplicationController def upload #取得上傳的檔案, #其自動會對應到ActionController::UploadedFile模組的物件 @uploaded_file = params[:file] #建立attachment model物件 @attachment = Attachment.new #取得原始檔名 @attachment.original_filename = @uploaded_file.original_filename if (@attachment.save) #取得副檔名,在與id組合存成新檔名 @extname = File.extname(@uploaded_file.original_filename) @filename = @attachment.id.to_s + @extname @attachment.filename = @filename #再次存檔至資料庫 @attachment.save #將檔案寫入file目錄下 File.open(Rails.root.join('file', @attachment.filename), 'wb') do |file| file.write(@uploaded_file.read) end #回傳attachment資訊 render json:@attachment else render json:@attachment.errors end end def download #依id取得attachment資訊 @attachment = Attachment.find(params[:id]) #透過send_file方法傳回此檔案, #透過:filename參數的設定, #將檔名變更回原始的檔名回傳。 send_file(Rails.root.join('file', @attachment.filename), :filename => @attachment.original_filename) end end

  • 當然,最後別忘了在attachment_service目錄下建立file目錄



4. 啟動伺服器 - rails server
  • 輸入rails server,透過rails server啟動伺服器,其預設的port為3000:


5. 測試 - Postman
  • 上傳檔案
    • URI:http://localhost:3000/attachments
    • HTTP Method:POST
    • Data:file => small_enginer.png

  • 下載檔案
    • URI:http://localhost:3000/attachments/1
    • HTTP Method:GET
    • Data:(none)




在此僅簡單的實作檔案上傳與下載的功能,若要完成RESTful SOA,請誤必考慮Error Handle這一部份。