Attachment_fu, PDF files and :db_file storage type

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

When you create a column with binary file type, Rails automatically
converts it to mysql type blob. By default blob type is 2kb. Use
t.column data, :binary, :limit => 10.megabyte
in your migration to set the correct size of the mysql blob.

  • Tushar

On Sep 7, 8:25 am, Roberto Gattinoni <rails-mailing-l…@andreas-