关于多个文件上传
上传多个文件上传参阅了
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/
http://www.flex888.com/2007/03/23/multiple-file-upload-with-ruby-on-rails-acts_as_attachment.html
http://www.flex888.com/2007/03/23/multiple-file-upload-with-ruby-on-rails-acts_as_attachment.html
其本上以最下面的为主,我主要用RMagick ,filecolumn,然后是上面连接中的 cool multiple file upload JavaScript 下面是是上面文章中的一段截取
First of all, go here to download the cool multiple file upload JavaScript so that you can have the dynamic file list built up for uploading, like this:
根据上面的提示下载好uplad javascript
放在自己的工程里,关于rmargick,filecolumn文件上传的搭配参考再建rmargick filecoumn一文,网上也比较多,
好了之后在view中加入
java 代码
- "my_file_element" type="file" name="file_1" >
- #在controller中加入
- i = 0
while @params["file_"+i.to_s] != "" and i <=2
p = Hash["entry"=>{"image"=>""}]
p["entry"]["image] = params["file_"+i.to_s] if i != 0
@entry = Entry.create!(p["entry"])
i += 1
end
就可以了,就可以实件文件上传了 - 如果你要一表多字段的话只要修改上面的代码就可以了,然后在 entry中
- file_column :image, :magick => {
:versions => { "thumb" => "50x50", "medium" => "640x480>" }
}
file_column :image1, :magick => {
:versions => { "thumb" => "50x50", "medium" => "640x480>" }
} - 增加相应的字段就可以了
此文多半属于抄阅,记下以便学习,请参考上面连接
关于如何使用file column 请参考
http://wiki.rubyonrails.org/rails/pages/HowToUseFileColumn
今天对数据库中的image字段进行了默认值 设置后,报 Trying to access file_column, but primary key got lost 错误,恢复为 null则正常
也可以把file_colum中 def self.sanitize_filename(filename) filename = File.basename(filename.gsub("\", "/")) # work-around for IE filename.gsub!(//,"") filename = "#{filename}" if filename =~ /^.+$/ filename = "unnamed" if filename.size == 0 filename end 根据自己的需要进行改进就可以了,比如说你想要不重复的可以用随机时间等一些方式来进行处理就可以了
应该将filename自定义为数字或者英文的名字。单独留一个字段,保存原文件名。
我这里是没有这种问题,但是对于中文命名的 他会以下划线代替,如 首页.jpg会心_____.jpg你在upload文件夹中,这个你可以在file cloumn的原文件中可以看到,自己可以修改 我的字符集为utf-8我想对于一些中文问题,你可以改成这个试试,或都filter定义中自己的字符集
你知道中文名文件上传异常的问题如何解决吗?就是那个Invalid argument exception