简体   繁体   English

在Rails 3中处理文件上传

[英]Handling file uploads in Rails 3

I had a working code for Rails 2 to handle file uploads that no longer works with Rails 3. The code is: 我有一个适用于Rails 2的工作代码来处理不再适用于Rails 3的文件上载。该代码是:

# Handling file uploads
def file=(file_data)
  unless file_data.blank?
    @file_data = file_data
    self.filename = file_data.original_filename
    self.size_before = file_data.size
  end
end

Now Rails 3 doesn't like that, complains with: 现在,Rails 3不喜欢这样,抱怨道:

undefined method `original_filename' for "MyFile.Ext":String

Any solutions that doesn't involve using a file attachment handler (Paperclip, etc)? 是否有不涉及使用文件附件处理程序(Paperclip等)的解决方案?

问题在于表单不是多部分表单。

Obviously, 'file_data' is a different type than in your old environment. 显然,“ file_data”是与旧环境不同的类型。 Here it's a String (which doesn't respond to 'original_filename'), but in your Rails 2 app it could have been StringIO. 这是一个String(不响应“ original_filename”),但是在您的Rails 2应用中,它可能是StringIO。

Try figuring how rails 3 handles binary data when posting a form by looking in your logs for params and/or debugging and investigating input in your receiving controller action. 尝试通过在日志中查找参数和/或调试并调查接收控制器操作中的输入来确定发布表单时Rails 3如何处理二进制数据。

Provide more context for more precise answers ;-) 提供更多上下文以获得更精确的答案;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM