开发者

PHP's tmp_name equivalent in RoR

开发者 https://www.devze.com 2022-12-12 15:25 出处:网络
Uploading a file in PHP results in a map. One of the keys, tmp_name, allows the user to manipulate the file without having to move it around. I\'d like to do the same for Ruby on Rails.

Uploading a file in PHP results in a map. One of the keys, tmp_name, allows the user to manipulate the file without having to move it around. I'd like to do the same for Ruby on Rails.

I have a form that takes a .csv file. With this, I'm trying to populate a database. However using just the file field from the for开发者_JAVA百科m, I get a type of ActionController::UploadedFile which doesn't allow me to do CSV.open or iterate over it. So how can I access the file uploaded without moving it into my project and then iterate over it? Thanks.


The method ActionController::UploadedFile#path should return the path of the tmp file on the server. You also have a handy #read method that returns the content of the file.

file_param = params[:upload][:file]
filename = file_param.original_filename
filename = file_param.path
filedata = file_param.read

CSV.open(file_param.path, "r") { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消