Rails重命名临时文件

编程入门 行业动态 更新时间:2024-10-16 15:58:34
本文介绍了Rails重命名临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 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重命名临时文件

本文发布于:2023-07-06 07:39:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   临时文件   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!