-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rb
63 lines (47 loc) · 1.74 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Attachment < ActiveRecord::Base
belongs_to :person
has_attachment :storage => :file_system, :max_size => 13.megabyte, :streaming => "false"
validates_as_attachment
# Read content of the attachment
def get_attachment_content
path = $attachments_location + self.public_filename
@data = ""
File.open(path, 'r+') do |f|
@data << f.read
end
return @data
end
# Read content of the attachment in base64 format
def get_attachment_content_in_base64
@data = get_attachment_content
return Base64.encode64(@data)
end
end
class AttachmentsController < ApplicationController
before_filter :login_required
def add_attachment
# get file name
file_name = params[:qqfile]
# get file content type
att_content_type = (request.content_type.to_s == "") ? "application/octet-stream" : request.content_type.to_s
# create temporal file
file = Tempfile.new(file_name)
# put data into this file from raw post request
file.print request.raw_post
# create several required methods for this temporal file
Tempfile.send(:define_method, "content_type") {return att_content_type}
Tempfile.send(:define_method, "original_filename") {return file_name}
# save file into attachment
attach = Attachment.new(:uploaded_data => file)
attach.person_id = params[:person_id]
attach.save!
respond_to do |format|
format.json{render(:layout => false , :json => {"success" => true, "data" => attach}.to_json )}
format.html{render(:layout => false , :json => attach.to_json )}
end
rescue Exception => err
respond_to do |format|
format.json{render(:layout => false , :json => {"error" => err.to_s}.to_json )}
end
end
end