强制链接下载 MP3 而不是播放它? [英] Force a link to download an MP3 rather than play it?
问题描述
我有一个锚链接
<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?
我正在阅读这篇文章.
def download
data = open(Song.first.attachment)
send_data data.read, :type => data.content_type, :x_sendfile=>true
end
文章建议使用 x_sendfile,因为 send_file 占用了一个 http 进程,可能会导致应用挂起直到下载完成.
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.
其次,我使用的是 send_data 而不是 send_file,如果文件是远程的(即托管在 Amazon S3 上),它似乎可以工作.正如这篇文章所建议的那样.
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.
我提到的那篇文章是2009年发表的.x_sendfile=>true还有必要吗?如果不包含它,它会挂起应用吗?
The article, I mentioned was posted on 2009. Is x_sendfile=>true still necessary? Will it hang the app if it isn't included?
我真的应该使用 send_data 还是 send_file?
Should I really be using send_data or send_file?
推荐答案
如果您不想使用 HTTP 服务器配置,您可以使用单独的控制器管理您的文件下载.
You can manage your file downloading with separate controller, if you don't want to eal with HTTP server configurations.
因此您可以send_file 将disposition
选项设为<代码>附件代码>.
So you can send_file with disposition
option as attachment
.
这篇关于强制链接下载 MP3 而不是播放它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!