根据RoR应用程序显示所有用户上传的文件文件名

时间:2018-01-16 20:32:18

标签: ruby-on-rails ruby file-upload download filenames

我想显示用户上传到我的Ruby-on-Rails应用程序的文件的所有文件名,以便管理员在下载文件之前查看文件名。所有文件都存储在目录中。

我的UploadController如下:

var verifyEmail = function(useremail) {
        return new Promise((resolve, reject) => {
            User.where('email', useremail)
            .fetch()
            .then(user => {
                if (user === null) {
                    reject(new error.ERROR_404());
                }
                resolve(user.toJSON());
            })
            .catch(err => {
                reject(new error.ERROR_500(err));
            })
        })
    }

    var recoverPassword = function(req, res) {
        var useremail = req.body.email;

        //verify email exists
        verifyEmail(useremail)
        .then(user => {
        })
        .catch(err => {
            res.status(err.status).json(err);
        })
    }

show.html.erb(我希望管理员能够看到上传的文件名并下载文件的视图)如下:

class UploadController < ApplicationController

  def index

  end

  def show

  end

  def uploadFile
    uploadedFile = params[:upload][:file]
    File.open(Rails.root.join('public','files', uploadedFile.original_filename), 'wb') do |f|
        f.write(uploadedFile.read)
    end
    redirect_to upload_path, :notice => "Thanks for your upload!"
  end

  def downloadFile
    fileName = params[:download][:file].original_filename
    send_file Rails.root.join('public','files', fileName)
  end   
end

这是我上传的index.html.erb

<h1>Download a file</h1>
<%= form_for :download, url: upload_admin_downloadFile_path do |f| %>
    <%= f.text_field :fileName %><br/>
    <br/>
    <%= f.submit :DOWNLOAD! %>
<% end %>

非常感谢所有帮助! 谢谢, 福克斯。

0 个答案:

没有答案