RoR:检查上传的文件 [英] RoR: Inspecting an Uploaded File
问题描述
在将上传的文件保存到文件系统之前,我需要检查它的位.PHP 的文档有一个很好的页面,它准确地告诉我哪些属性可供我使用(http://us3.php.net/manual/en/features.file-upload.post-method.php),但我找不到 Ruby 和/或 Rails.
I need to inspect the bits of an uploaded file before it's ever saved off to the file system. PHP's documentation has a nice page that tells me exactly what properties are available for me to use (http://us3.php.net/manual/en/features.file-upload.post-method.php), but I can't find something similar for Ruby and/or Rails.
我也尝试记录上传的 JSON 格式的字符串,但这只会给我一个多余的 UTF-8 错误.我想不出还有什么可以尝试的.
I've also tried logging a JSON-formatted string of the upload, but that just gives me a redundant UTF-8 error. I can't think of anything else to try.
谁能提供任何见解或将我指向正确的地方?
Can anyone offer any insight or point me to the right place?
谢谢.
更新:我正在 OS X (Leopard) 上运行 Apache 2.2.11,以防彼得是对的(见下文).
UPDATE: I'm running Apache 2.2.11 on OS X (Leopard) in case Peter is right (see below).
更新:如果有帮助,我的输入参数被记录为 "upload"=>#<File:/tmp/RackMultipart.64239.1>
.我只是不确定如何访问它以获取它的部分".
UPDATE: In case it helps, my input parameter is logged as "upload"=>#<File:/tmp/RackMultipart.64239.1>
. I'm just not sure how to access it to get to its "parts".
推荐答案
据我所知或发现,在读取上传之前没有物理文件.这与 derfred 的回复一致.可以访问的唯一元数据是:
As far as I've been able to tell or find, there is no physical file until an upload is read. This is inline with derfred's reply. The only metadata that can be accessed is:
uploaded_file.content_type # the uploaded file's MIME type
uploaded_file.original_path # which is really just the name of the file
此外,uploaded_file
上有一个 read
方法,允许访问文件的内容,并可能将其写入永久文件系统.
Additionally, there's a read
method on the uploaded_file
that allows the file' content to be accessed and, presumably, written to the permanent file system.
我注意到的另一件事是,only 意味着 Ruby 提供检查文件的 MIME 类型位于上传文件的此 content_type
属性中.一旦文件位于文件系统上,就无法再访问或确定 MIME 类型.
Something else that I've noticed is that the only means Ruby offers to inspect the MIME type of a file is in this content_type
property of an uploaded file. Once the file is on the file system, there's no longer any way of accessing or determining the MIME type.
这篇关于RoR:检查上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!