Problem:
- correctly uploading PDF files, PNG, JPEG and storing in MySQL dbase;
- when downloading (and it seems only with PDF files) with send_data and
saving on file system, consistently only 2kbyte of data are downloaded;
if alternatively :disposition => ‘inline’ Adobe Reader signals incorrect
file content.
Has anyone experienced the same problem and what can be wrong with the
following?
My migrations:
class CreateDocmetas < ActiveRecord::Migration
def self.up
create_table :docmeta do |t|
t.column :db_file_id, :integer
t.column :size, :integer, :null => false
t.column :content_type, :text, :null => false
t.column :filename, :text, :null => false
t.column :height, :integer
t.column :width, :integer
t.column :parent_id, :integer
t.column :thumbnail, :text
end
end
def self.down
drop_table :docmeta
end
end
class CreateDbFiles < ActiveRecord::Migration
def self.up
create_table :db_file do |t|
t.column :data, :binary
end
end
def self.down
drop_table :db_file
end
end
My models:
class Docmeta < ActiveRecord::Base
set_table_name ‘docmeta’
belongs_to :db_file
has_attachment :storage => :db_file,
:min_size => 1.kilobyte,
:max_size => 10.megabytes
validates_as_attachment
end
class DbFile < ActiveRecord::Base
set_table_name ‘db_file’
end
My controller (only for the download action):
def view
@attachment = Attachment.find(params[:attchid])
if @attachment
send_data @attachment.docmeta.db_file.data,
:filename => @attachment.docmeta.filename,
:type => @attachment.docmeta.content_type,
:disposition => ‘attachment’
end
end