Ive got an anchor link
我有一个锚链接
<a href="http://bucket_name.amazonaws.com/uploads/users/4/songs/7/test.mp3">Download</a>
How do I make it so when a user clicks on it, it actually opens a popup asking the user to save the file instead of trying to play the file on the browser?
当用户点击它时,如何实现它,它实际上会打开一个弹出窗口,要求用户保存文件而不是尝试在浏览器上播放文件?
EDIT:
编辑:
I was reading this article.
我正在读这篇文章。
def download
data = open(Song.first.attachment)
send_data data.read, :type => data.content_type, :x_sendfile=>true
end
The article suggest using x_sendfile, since send_file takes up an http process with the potential risk of hanging the app until the download is completed.
本文建议使用x_sendfile,因为send_file会占用一个http进程,可能会挂起应用程序直到下载完成。
Second, I am using send_data instead of send_file, which seems to work if the file is remote (i.e. hosted on Amazon S3). As suggested by this article.
其次,我使用send_data而不是send_file,如果文件是远程的(即在Amazon S3上托管),这似乎有效。正如本文所述。
The article, I mentioned was posted on 2009. Is x_sendfile=>true still necessary? Will it hang the app if it isn't included?
我提到的这篇文章是在2009年发布的。是否还需要x_sendfile => true?如果不包含应用程序,它会挂起吗?
Should I really be using send_data or send_file?
我真的应该使用send_data还是send_file?
3 个解决方案
#1
8
You can manage your file downloading with separate controller, if you don't want to eal with HTTP server configurations.
如果您不想使用HTTP服务器配置,则可以使用单独的控制器管理文件下载。
So you can send_file with disposition
option as attachment
.
因此,您可以将带有处置选项的send_file作为附件。
#2
5
Depends on how you / where you serve the file itself. I do not have experience with ruby but if you can alter the headers(most platforms offer this option) of the http response you can force a download. This requires:
取决于您/文件本身的服务方式。我没有使用ruby的经验,但如果您可以更改http响应的标题(大多数平台提供此选项),您可以强制下载。这需要:
Content-Type: application/force-download
I guess it will use "Content-type: application/octet-stream" by default which will cause the browser to play it.
我猜它默认会使用“Content-type:application / octet-stream”,这会导致浏览器播放它。
But this will only work if you have control over the server/location that holds the actual file since you need to change the response when the file is sent to the browser.
但这只有在您控制保存实际文件的服务器/位置时才有效,因为您需要在将文件发送到浏览器时更改响应。
#3
2
Skip The Controller Action
You don't even need the download
controller action, you can just generate a download-friendly link like so:
您甚至不需要下载控制器操作,您只需生成一个下载友好的链接,如下所示:
In your attachment.rb
def download_url
S3 = AWS::S3.new.buckets[ 'bucket_name' ] # This can be done elsewhere as well,
# e.g config/environments/development.rb
url_options = {
expires_in: 60.minutes,
use_ssl: true,
response_content_disposition: "attachment; filename=\"#{file_name}\""
}
S3.objects[ self.path ].url_for( :read, url_options ).to_s
end
In your views
<%= link_to 'Download Avicii by Avicii', attachment.download_url %>
If you still wanted to keep your download
action for some reason then just use this:
如果由于某种原因仍想保留下载操作,请使用以下命令:
In your attachments_controller.rb
def download
redirect_to @attachment.download_url
end
Thanks to guilleva for his guidance.
感谢guilleva的指导。
#1
8
You can manage your file downloading with separate controller, if you don't want to eal with HTTP server configurations.
如果您不想使用HTTP服务器配置,则可以使用单独的控制器管理文件下载。
So you can send_file with disposition
option as attachment
.
因此,您可以将带有处置选项的send_file作为附件。
#2
5
Depends on how you / where you serve the file itself. I do not have experience with ruby but if you can alter the headers(most platforms offer this option) of the http response you can force a download. This requires:
取决于您/文件本身的服务方式。我没有使用ruby的经验,但如果您可以更改http响应的标题(大多数平台提供此选项),您可以强制下载。这需要:
Content-Type: application/force-download
I guess it will use "Content-type: application/octet-stream" by default which will cause the browser to play it.
我猜它默认会使用“Content-type:application / octet-stream”,这会导致浏览器播放它。
But this will only work if you have control over the server/location that holds the actual file since you need to change the response when the file is sent to the browser.
但这只有在您控制保存实际文件的服务器/位置时才有效,因为您需要在将文件发送到浏览器时更改响应。
#3
2
Skip The Controller Action
You don't even need the download
controller action, you can just generate a download-friendly link like so:
您甚至不需要下载控制器操作,您只需生成一个下载友好的链接,如下所示:
In your attachment.rb
def download_url
S3 = AWS::S3.new.buckets[ 'bucket_name' ] # This can be done elsewhere as well,
# e.g config/environments/development.rb
url_options = {
expires_in: 60.minutes,
use_ssl: true,
response_content_disposition: "attachment; filename=\"#{file_name}\""
}
S3.objects[ self.path ].url_for( :read, url_options ).to_s
end
In your views
<%= link_to 'Download Avicii by Avicii', attachment.download_url %>
If you still wanted to keep your download
action for some reason then just use this:
如果由于某种原因仍想保留下载操作,请使用以下命令:
In your attachments_controller.rb
def download
redirect_to @attachment.download_url
end
Thanks to guilleva for his guidance.
感谢guilleva的指导。