按照说明一步一步来,首先声明了一个model,用来保存图片信息,顺便在controller中实现图片上传,显示等功能,定义的model如下:
ruby 代码
- class Upfile < ActiveRecord::Base
- has_attachment :content_type=>:image,:max_size=>2.megabyte,:thumbnails=>{:small=>'200x200>', :middle=>'400x400>'},:storage=>:db_file
- validates_as_attachment
- end
这里我没有如例子给出的一般采用文件系统,而是使用了数据库来保存图像(如果需要保存在数据库中,需要另外定义一个model和一个数据表,Mode名字叫做DbFile)。各个参数的意义如下:
This method accepts the options in a hash:
:content_type # Allowed content types.
# Allows all by default. Use :image to allow all standard image types.
:min_size # Minimum size allowed.
# 1 byte is the default.
:max_size # Maximum size allowed.
# 1.megabyte is the default.
:size # Range of sizes allowed.
# (1..1.megabyte) is the default. This overrides the :min_size and :max_size options.
:resize_to # Used by RMagick to resize images.
# Pass either an array of width/height, or a geometry string.
:thumbnails # Specifies a set of thumbnails to generate.
# This accepts a hash of filename suffixes and RMagick resizing options.
# This option need only be included if you want thumbnailing.
:thumbnail_class # Set which model class to use for thumbnails.
# This current attachment class is used by default.
:path_prefix # path to store the uploaded files.
# Uses public/#{table_name} by default for the filesystem, and just #{table_name} for the S3 backend.
# Setting this sets the :storage to :file_system.
:storage # Specifies the storage system to use..
# Defaults to :db_file. Options are :file_system, :db_file, and :s3.
:processor # Sets the image processor to use for resizing of the attached image.
# Options include ImageScience, Rmagick, and MiniMagick. Default is whatever is installed.
Examples:
has_attachment :max_size => 1.kilobyte
has_attachment :size => 1.megabyte..2.megabytes
has_attachment :content_type => 'application/pdf'
has_attachment :content_type => ['application/pdf', 'application/msword', 'text/plain']
has_attachment :content_type => :image, :resize_to => [50,50]
has_attachment :content_type => ['application/pdf', :image], :resize_to => 'x50'
has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :file_system, :path_prefix => 'public/files'
has_attachment :storage => :file_system, :path_prefix => 'public/files',
:content_type => :image, :resize_to => [50,50]
has_attachment :storage => :file_system, :path_prefix => 'public/files',
:thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :s3
接下来定义Model对应的Migration
Upfile:
ruby 代码
- class CreateUpfiles < ActiveRecord::Migration
- def self.up
- create_table :upfiles do |t|
- t.column :size,:integer,:null=>false
- t.column :content_type,:string,:null=>false
- t.column :filename,:string,:null=>false
- t.column :height,:integer
- t.column :width,:integer
- t.column :parent_id,:integer
- t.column :thumbnail,:string
- t.column :db_file_id,:integer
- end
- end
-
- def self.down
- drop_table :upfiles
- end
- end
DbFile:
ruby 代码
- class CreateDbFiles < ActiveRecord::Migration
- def self.up
- create_table :db_files do |t|
- t.column :data, :binary
- end
- end
-
- def self.down
- drop_table :db_files
- end
- end
各个字段对应的解释如下:
Fields for attachment_fu metadata tables...
in general:
size, :integer # file size in bytes
content_type, :string # mime type, ex: application/mp3
filename, :string # sanitized filename
that reference images:
height, :integer # in pixels
width, :integer # in pixels
that reference images that will be thumbnailed:
parent_id, :integer # id of parent image (on the same table, a self-referencing foreign-key).
# Only populated if the current object is a thumbnail.
thumbnail, :string # the 'type' of thumbnail this attachment record describes.
# Only populated if the current object is a thumbnail.
# Usage:
# [ In Model 'Avatar' ]
# has_attachment :content_type => :image,
# :storage => :file_system,
# :max_size => 500.kilobytes,
# :resize_to => '320x200>',
# :thumbnails => { :small => '10x10>',
# :thumb => '100x100>' }
# [ Elsewhere ]
# @user.avatar.thumbnails.first.thumbnail #=> 'small'
that reference files stored in the database (:db_file):
db_file_id, :integer # id of the file in the database (foreign key)
Field for attachment_fu db_files table:
data, :binary # binary file data, for use in database file storage
搞定了数据库,接下来让我们回到controller,在UpfileController中定义几个方法:upload,showimage,image,upload用来控制上传文件,showimage用来显示一幅图片,image用来得到某幅图片数据。一下是三个action的代码:
ruby 代码
- class UpfileController < ApplicationController
- def upload
- if request.post?
- @attachable_file = Upfile.new(params[:attachment_metadata])
- if @attachable_file.save
- flash[:notice] = 'Attachment was successfully created.'
- redirect_to :action=>:showimage,:id=>@attachable_file.attributes["id"]
- end
- end
- end
-
- def showimage
- @image = Upfile.find(:first,:conditions=>{:id=>params[:id],:parent_id=>nil})
- end
-
- def image
- @image = Upfile.find(:first,:conditions=>{:id => params[:id]})
- send_data(@image.db_file.data,:filename=>@image.attributes["filename"],:type=>@image.attributes["content_type"],:disposition=>"inline")
- end
- end
upload.rhtml:
ruby 代码
- <h1>Upfile
- <% form_for(:attachment_metadata, :url => {:action=>"upload"}, :html => {:multipart=>true}) do |form| -%>
- select image to upload: <%= form.file_field :uploaded_data %><p>
- <%= submit_tag "upload now" %>
- <% end -%>
showimage.rhtml
ruby 代码
- <% if @image -%>
- <% if @image.image? -%>
- <img src="<%= url_for(:action=>:image,:id=>@image.attributes["id"]) %>" />
- <% end -%>
- <% end -%>
经过这样设置,在upload一个图像后,会自动转到刚上传图像显示的页面。
不过经过我现在实践,这个方法还有一些问题,主要就是读取数据库中图像时,若图像比较大,则只能读取前65535个字节的数据,另外,@image.db_file.data.class是string
分享到:
相关推荐
AT_Attachment_with_Packet_Interface_-_7_Volume_3
_storage_emulated_0_android_data_com.tencent.mm_MicroMsg_517174082dbc007f25c5bd836bdd4446_attachment_段润昌_648.wps
在MATLAB编程环境中,"H_attachment_comprose_matlab_"这个标题暗示了我们正在处理一个与图像处理或图形绘制相关的任务,特别是与生成特定形状的方位标志有关。方位标志通常用于图表中,以便清晰地指示出方向或者...
ATA接口的详细解读,working draft proposed American National Standard for Information Systems - ATA (ATAttachment) 78页
标题中的"attachment_1487958_16b_win64_2017-05-10"很可能是一个软件安装包或更新文件,尤其考虑到它与MATLAB 2016相关。这个文件名包含了几个关键信息:首先,“16b”可能表示这是MATLAB的一个版本号,可能是R2016...
当接收到数据时,接收方同样使用该生成多项式来检验数据的完整性和一致性。如果计算出的CRC与接收到的CRC匹配,那么数据很可能没有错误;如果不匹配,就表明可能在传输过程中发生了错误。 在LTE系统中,CRC的使用...
Information Technology - AT Attachment with Packet Interface - 6 (ATA/ATAPI-6)ATA_ATAPI-6标准规范,驱动开发参考文档
"bugzilla_attachment_viewer" 是一个针对 Bugzilla 平台的 Chrome 浏览器扩展程序,它的主要功能是让用户能够在浏览器中直接内联查看图像附件,而无需下载这些文件到本地。这极大地提高了用户在处理 Bugzilla 中的...
#!/usr/bin/env python # coding: utf-8 # In[1]: import pandas as pd # Load the provided ...merged_data = pd.merge(attachment_2, attachment_1, on="单品编码", how="left") # Display the first few rows
very interesting matlab hev model
在IT行业中,"Attachment_Project:附件项目"是一个可能与文件管理和Web应用相关的项目。这个项目的描述非常简洁,只提到了“附件项目”这个名字,没有提供具体的功能或目标。不过,结合给出的标签“HTML”,我们可以...
西门子840d数控系统说明。对方的更多更好
"attachment_finder_app" 是一个基于JavaScript开发的简单应用,主要用于帮助用户管理和标记带有附件的票证。这个应用程序的独特之处在于它允许用户自定义标签,这些标签可以方便地应用于各种票证,进而使得在报告、...
通过编写 Rectangle 类,可以了解如何定义类、如何使用成员变量和成员方法,以及如何实践面向对象编程的基本原则。 圆类 Circle 圆类 Circle 是一个基本的几何图形类,拥有一个成员变量 radius,表示圆的半径。该...
attachment_doc是一个SquirrelMail插件,允许用户使用其浏览器查看电子邮件中的文档附件。 该插件将文档转换为html格式。 目前支持MSWord(DOC)和可移植文档格式(PDF)!
"backlog_attachment_alert" 是一个针对Backlog平台的Chrome扩展程序,它的主要功能是在用户创建问题或发表评论时提供附件提醒服务,确保用户不会遗漏任何重要的文件上传。这个扩展程序特别适用于那些依赖Backlog...
标题中的"566223_ATTACHMENT01_filamentwinding_filament_zip_"似乎是一个文件名,其中包含了关键词"filament winding",这通常是指一种制造复合材料管状或圆柱形结构的技术。该技术涉及将连续纤维(如碳纤维或玻璃...
Attachment 1_chazhi.xlsx
REAL_HOMES_slider_image :获取一张图像的attachment_id。 REAL_HOMES_page_banner_image :获取一张图像的attachment_id。 REAL_HOMES_property_price :常规文本字段-仅数字。 值范例:435000 REAL_HOMES_...
【标题解析】:“attachment_repo:我要分享的一些文件”这个标题表明这是一个分享的资源集合,主要可能包含各种与“attachment_repo”相关的文件。由于没有具体的上下文,我们可以理解为这是一个个人或者团队分享的...