Tag: file upload

上传器产生有关应存储路径的列的错误

Image模型与Organization模型具有1:1的关联。 在组织控制器中, create方法调用名为upload_file的Image模型方法。 def create @organization = Organization.new(new_params) if @organization.save Image.upload_file(@organization.id) end end upload_file方法使用carrierwave上传器将标准文件存储在Amazon S3存储桶中。 为此,Image模型包括mount_uploader :file_name, ImageUploader 。 我的问题是如何为上传的文件创建一个Image实例? 存储文件的路径应存储在Image模型的列file_name中。 与图像关联的组织应存储在Image模型的organization_id列中。 我怎样才能做到这一点? 更具体地说,我应该为下面的模型方法添加什么代码? (另请参阅以下方法中的注释。 def self.upload_file(organization_id) file = ‘app/assets/emptyfile.xml’ uploader = ImageUploader.new uploader.store!(file) # Am I correct to assume that the previous line uploads the file using the uploader, but does not yet create […]

使用Ruby将大文件上传到S3失败,出现内存不足错误,如何在块中读取和上载?

我们通过Ruby AWS SDK(v2)从Windows机器上传各种文件到S3。 我们已经使用Ruby 1.9进行了测试。 我们的代码工作正常,除非遇到大文件,抛出内存不足错误。 起初我们使用以下代码将整个文件读入内存: :body => IO.binread(filepath), 然后在Googling之后我们发现有很多方法可以用Ruby读取文件: :body => File.open(filepath, ‘rb’) { |io| io.read }, 但是这段代码没有解决问题,我们找不到特定的S3(或相关)示例,它显示了如何读取文件并以块的forms传递给S3。 整个文件仍然加载到内存中,并在大文件中引发内存不足错误。 我们知道我们可以将文件拆分为块并使用AWS多部件上传上传到S3,但是如果可能的话,首选是避免这种情况(尽管如果这是唯一的方法就没问题)。 我们的代码示例如下。 以块的forms读取文件,避免内存不足错误以及上传到S3的最佳方法是什么? require ‘aws-sdk’ filepath = ‘c:\path\to\some\large\file.big’ bucket = ‘s3-bucket-name’ s3key = ‘some/s3/key/file.big’ accesskeyid = ‘ACCESSKEYID’ accesskey = ‘ACCESSKEYHERE’ region = ‘aws-region-here’ s3 = Aws::S3::Client.new( :access_key_id => accesskeyid, :secret_access_key => accesskey, :region => […]

Carrierwave无法删除图像

我现在正在使用Carrierwave上传图片。 一切都很好,除了一个, – 当我添加复选框以删除上传的图像时,我收到错误:“无法批量分配受保护的属性:remove_image”。 形成: … … 模型: class Manufacturer /^[az\d\-]*$/, :message => “az, 0-9 \’ – \’ ONLY” default_scope order(‘position ASC’) before_destroy :remember_image after_destroy :remove_img protected def remember_image @image_name = self[:image] end def remove_img File.delete(“#{Rails.root}/public/images/manufacturer/#{@image_name}”) File.delete(“#{Rails.root}/public/images/manufacturer/thumb_#{@image_name}”) end end 提供者: class ManufacturerUploader [300, 300] version :thumb do process :resize_to_fit => [150, 150] end def extension_white_list […]

Carrierwave如何获取文件扩展名

我正在开发一个需要文件上传/下载的Ruby on Rails应用程序。 对于上传部分,我使用了gem carrierwave,因为它非常易于使用且灵活。 问题是:一旦我上传了文件,我需要知道一些事情:即如果它是pdf而不是下载文件我将其显示为内联,并且图像也是如此。 我如何获得文件扩展名 ,我该怎么做才能将文件 发送给用户? 任何反馈表示赞赏谢谢!!

使用Ruby 1.8.7的未定义方法`has_attached_file’,带有paperclip 2.3.8 gem for Rails 2

我正在尝试使用我的rails应用程序中的paperclip插件上传post的图像。 我在Ubuntu 10.04操作系统上使用Rails 2.0.2和ruby 1.8.7用于项目特定目的。 我指的是以下教程: – http://railscasts.com/episodes/134-paperclip?view=comments http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails.html 我通过以下方式在paperclip插件上做了一个git clone: – https://github.com/thoughtbot/paperclip ,但像ruby script/generate paperclip group_post photo这样的命令没有为它生成迁移。 然后,我通过ruby脚本/生成创建了相同的迁移,并上传了我的表group_posts的相应列。 然后我尝试安装gem,因为我使用activerecord和activesupport 2.0.2我使用了rubygems.org的paperclip 2.3.8 gem。未定义的方法错误仍然存​​在,我真的无法弄清楚为什么。 现在这里..命令ruby script/generate paperclip group_post photo对我来说非常好。 我得到的当前错误看起来像这样: – undefined method `has_attached_file’ for # 我观察到这个行为的一个有趣的事情就是当我使用IRB时, require ‘paperclip’返回true但我想知道为什么他们无法检测我的rails应用程序中插件附带的方法。 该应用程序跟踪是: – /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in `method_missing_without_paginate’ vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in `method_missing’ app/models/group_post.rb:9 app/controllers/groups_controller.rb:27:in `show’ 以下是完整跟踪 : – /home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in `method_missing_without_paginate’ vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in `method_missing’ app/models/group_post.rb:9 […]

回形针video上传

我正在尝试在我的post中启用video上传function。 无法让它显示video。 video上传,我可以确认,虽然我设法右键点击video区域并下载它。 问题是如何正确查看它。 移民: class AddAttachmentVideoToPosts < ActiveRecord::Migration def self.up change_table :posts do |t| t.attachment :video end end def self.down drop_attached_file :posts, :video end end def change create_table :videos do |t| t.string :video_file_name t.string :video_content_type t.integer :video_file_size t.datetime :video_updated_at t.timestamps end end 发布模型 class Post ‘created_at desc’ attr_accessible :content, :title, :photo, :photo_delete, :video, :video_delete, […]

使用CarrierWave上传RESTful文件

我正在尝试为文件上传构建API后端。 我希望能够使用具有Base64编码的文件字符串的POST请求上传文件。 服务器应解码字符串,并使用CarrierWave保存文件。 这是我到目前为止所拥有的: photo.rb: class Photo include Mongoid::Document include Mongoid::Timestamps mount_uploader :image_file, ImageUploader end image_uploader.rb: class ImageUploader < CarrierWave::Uploader::Base storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end end Rails控制台:(摘要) ruby-1.8.7-p334 :001 > img = File.open(“../image.png”) {|i| i.read} => “\377 JFIF\000\001\002\001\000H\000H\000\000\377 Photoshop 3.0\0008BIM\003… ruby-1.8.7-p334 :003 > encoded_img = Base64.encode64 img => 3af8A\nmLpplt5U8q+a7G2… ruby-1.8.7-p334 :005 > p = […]

Carrierwave或Dragonfly

我一直在研究rails文件上传工具,对我来说最吸引人和最有趣的是carrierwave和dragonfly。 从环顾四周来看,似乎载波采用更传统的风格,您可以在保存时处理文件,而蜻蜓是中间件,因此它允许您即时处理。 我想知道人们是否有任何参考性能测试或任何比较两者的测试。 此外,只是好奇人们的意见是什么,他们喜欢什么,当然为什么他们喜欢它。

使用Web套接字进行点对点文件共享

这是一个理论问题,但是,我需要为我的基于Web套接字的聊天应用程序添加文件共享function。 我可以使用像Amazon S3这样的服务通过发布文件的链接来上传要共享的文件,但这涉及上传可能已经可以通过本地网络访问的文件(例如,在同事之间共享文件) 。 所以我认为有可能通过现有的Web套接字连接以某种方式隧道上传/下载/传输。 但是,我对HTTP文件传输知之甚少,不知道如何实现它的下一步。 Web套接字是否存在限制,以防止这种情况发生? 我正在使用Ruby和EventMachine来实现我当前的Web套接字。 如果您能够提供高水平的概述以帮助我入门,那将非常感激。