客戶端為 Cordova
伺服器為 Ruby on Rails
將圖片轉為base64使用www-form傳送至伺服器
Cordova - javascript
$.ajax({
url: strServerURI,
data: {
//connect_token 索取儲存的會員TOKEN
connect_token: window.localStorage.getItem("member_token"),
sticker: "data:image/jpeg;base64," + imageURI
},
cache: false,
contentType: "application/x-www-form-urlencoded",
type: 'POST',
success: function () {
alert("頭貼上傳成功");
},
error: function () {
alert("頭貼上傳失敗");
}
});
Ruby on rails - controllers
class Api::V1::Members::StickerController < Api::V1::ApiController
//驗證會員TOKEN
before_action :verify_member_connect_token
def create
@current_member.sticker_path = params[:sticker]
@current_member.save!
@sticker_path = @current_member
end
end
Ruby on Rails - routes
#API導入網址
defaults format: :json do
namespace :api do
namespace :v1 do
# 上傳頭貼
match 'sticker', to: 'members/sticker#create', via: :post
end
end
end