Rails重命名临时文件 [英] Rails Rename tempfile
问题描述
我正在使用 roo-rb 来访问上传的文件.我的代码是这样的:
I am using roo-rb for accessing the uploaded files. My code is like this:
s = Roo::Excelx.new(params[:upload][:file].tempfile.path)
但是我遇到了问题,因为生成的临时文件没有扩展名,并且出现了这个异常:
But I am having problem with it because the generated tempfile has no extension and I'm having this exception:
.../AppData/Local/Temp/RackMultipart20150216-10192-13yn50s不是Excel-xlsx文件
.../AppData/Local/Temp/RackMultipart20150216-10192-13yn50s is not an Excel-xlsx file
是否可以重命名 tempfile.path
,使其具有适当的扩展名(xlsx)?还是有一种更优雅的方式来解决这个问题?
Is there a way to rename the tempfile.path
so that it will have a proper extension (xlsx)? Or is there a more elegant way to solve this problem?
推荐答案
我已经使用 fileutils
成功重命名了一个临时文件(即使部署在Heroku上).这是代码:
I have successfully renamed a temporary file (even when deployed on Heroku) using the fileutils
. Here is the code:
require 'roo'
require 'fileutils'
tmp = params[:upload][:file].tempfile
file = File.join("public", params[:upload][:file].original_filename)
FileUtils.cp tmp.path, file
s = Roo::Excelx.new(file)
这篇关于Rails重命名临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!