简体   繁体   English

我可以包含一个按钮来下载电子邮件中的附件吗?

[英]Can I include a button to download an attachment within an email?

I'm developing a rails application and using ActionMailer to send emails to the users. 我正在开发一个Rails应用程序,并使用ActionMailer向用户发送电子邮件。 I know how to include attachments in every email. 我知道如何在每封电子邮件中包含附件。 But I was wondering if I can provide a button within the mailer view to download explicitly a particular attachment as a user interface improvement. 但是我想知道是否可以在邮件视图中提供一个按钮来显式下载特定附件,以改进用户界面。

Update: 更新:

I honestly forgot to mention something important about this. 老实说,我忘了提到一些重要的事情。 The eventual endpoint that I need to build in order to download the file from the email needs to be authenticated via Authorization header, and it's value is an encoded token using base64. 为了从电子邮件中下载文件,我需要构建的最终端点需要通过Authorization标头进行身份验证,它的值是使用base64编码的令牌。 I don't know if there is a vulnerability by sending this token within the email. 我不知道通过在电子邮件中发送此令牌是否存在漏洞。

Sure. 当然。 The button can link to a controller action in your rails application that sends the file down to the user just as if the user clicked to download it when browsing in your app. 该按钮可以链接到Rails应用程序中的控制器操作,该操作将文件发送给用户,就像用户在您的应用程序中浏览时单击下载文件一样。

Do this in route.rb route.rb执行此route.rb

get "controller/action" , as: :download

in mailer view, add this 在邮件视图中,添加此

<%= link_to "DOWNLOAD", download_path %>

in your controller do something like 在您的控制器中执行类似

def download
    send_file 'path_to_your_file',:type=>"application/extension_of_your_file", :x_sendfile=>true 
end

The Other way 另一种方法

You can simply put your file in public folder and then just put a link to download in your mailer view like: 您可以简单地将文件放在公用文件夹中,然后在邮件视图中放置一个下载链接,例如:

<%= link_to 'name of button', 'http:// your_website / your_file_name' %>

Also next time make sure to add some code to your Question about what you have tried or what you are planning to achieve your question. 下次还要确保将一些代码添加到您的问题中,以了解您尝试过的内容或计划要解决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM