【问题标题】:Access a file upload without saving the file, and then emailing it as an attachment在不保存文件的情况下访问文件上传,然后将其作为附件通过电子邮件发送
【发布时间】:2012-03-17 01:40:37
【问题描述】:

我正在尝试制作一个表单,该表单将使用 Action Mailer 发送带有附件的电子邮件。我没有使用模型来支持我正在上传的对象。我想将文件直接附加到邮件中,而不必将其保存到服务器硬盘上。在我的控制器中:

def create
    attachment = params[:attachment].read
    ApplicationRequestMailer.send_application_to_be_entered(current_user.member, attachment).deliver
    render :nothing => true
end

在我的邮件中:

class ApplicationRequestMailer < ActionMailer::Base

    def send_application_to_be_entered(member, file)
        attachment[file.origional_name] = file.read
        mail(:to => 'test@test.com', :subject => "To Be Entered")
    end

end

有没有办法做到这一点?还是我需要先使用回形针之类的东西保存文件?

【问题讨论】:

  • 您尝试过这种方法吗?我认为它应该可以工作,除了你正在读取数据两次......

标签: ruby-on-rails ruby-on-rails-3 actionmailer


【解决方案1】:

不确定这是否完全正确,但它正在工作:

def create
    ApplicationRequestMailer.send_application_to_be_entered(params[:application].read(), params[:application].original_filename).deliver
    redirect_to dashboards_path, :notice => "Request Sent."  
end


def send_application_to_be_entered(file, filename)
    attachments[filename] = file
    mail(:to => 'test@test.com', :subject => "Application To Be Entered")
end

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2014-07-20
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多