Ruby-on-rails Rails 3 - 临时文件路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4357174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:48:44  来源:igfitidea点击:

Rails 3 - Tempfile Path?

ruby-on-railsruby-on-rails-3

提问by AnApprentice

I have the following:

我有以下几点:

attachments.each do |a|
   Rails.logger.info a.filename
   tempfile = Tempfile.new("#{a.filename}", "#{Rails.root.to_s}/tmp/")
   Rails.logger.info tempfile.path
end

Where attachments is from paperclip.

附件来自回形针。

Here's the output:

这是输出:

billgates.jpg
/Users/bhellman/Sites/cline/tmp/billgates.jpg20101204-17402-of0u9o-0

Why is the file name getting 20101204-17402-of0u9o-0appended to at the end? That's breaking everything with paperclip etc. Anyone seen this before? For the life of I have no idea what's doing it?

为什么文件名被20101204-17402-of0u9o-0附加到最后?用回形针等打破了一切。有人见过这个吗?我这辈子都不知道是干什么的?

Thanks

谢谢

UPDATEPaperclip: Paperclip on github

更新回形针:github 上的回形针

a is the attachment file

a是附件文件

tempfile = Tempfile.new("#{a.filename}", "#{Rails.root.to_s}/tmp/")
tempfile << a.body
tempfile.puts
attachments.build(
    :attachment => File.open(tempfile.path)
)

回答by andistuder

best make sure your tempfile has the correct extension, saving you to trying and change it after:

最好确保您的临时文件具有正确的扩展名,以免您在以下情况下尝试和更改它:

file = Tempfile.new(['hello', '.jpg'])

file.path # => something like: "/tmp/hello2843-8392-92849382--0.jpg"

file = Tempfile.new(['你好', '.jpg'])

file.path # => 类似:“/tmp/hello2843-8392-92849382--0.jpg”

more here: http://apidock.com/ruby/v1_9_3_125/Tempfile/new/class

更多信息:http: //apidock.com/ruby/v1_9_3_125/Tempfile/new/class

回答by drummondj

The first argument for Tempfile.new is just a basename. To make sure each Tempfile is unique the characters are appended to the end of the file.

Tempfile.new 的第一个参数只是一个基本名称。为确保每个 Tempfile 都是唯一的,将字符附加到文件末尾。

回答by yfeldblum

You should use Paperclip's API for this:

您应该为此使用 Paperclip 的 API:

tempfiles = []
attachments.each do |a|
  # use Attachment#to_file to get a :filesystem => file, :s3 => tempfile
  tempfiles << a.to_file
end

tempfiles.each do |tf|
  Rails.logger.debug tf.filename
end

回答by yfeldblum

attachment = attachments.build(
  :attachment => File.open(tempfile.path)
)

# change the displayed file name stored in the db record here
attachment.attachment_file_name = a.filename # or whatever else you like

attachment.save!

回答by Chris Nolet

The best way I found to deal with this was to specify the file extension in the Paperclip attribute. For example:

我发现处理这个问题的最好方法是在 Paperclip 属性中指定文件扩展名。例如:

has_attached_file :picture,
  :url => "/system/:hash.jpg",
  :hash_secret => "long_secret_string",
  :storage => :s3,
  :s3_credentials => "#{Rails.root}/config/s3.yml"

Note that the :url is declared as '.jpg' rather than the traditional .:extension.

请注意:url 被声明为“.jpg”而不是传统的 .jpg .:extension

Good luck!

祝你好运!