使用Amazon S3在Chrome,Firefox,Safari和IE中强制下载MP3

时间:2018-07-24 10:39:01

标签: c# asp.net-mvc amazon-s3 browser http-headers

有没有一种方法可以强制从Amazon S3下载MP3文件。

我的Razor中有一个“下载”按钮:

 <td>
                <a href="@t.S3PreSignedUrl" class="js_recordingDownloadButton document-link btn btn-info btn-block br2 btn-xs fs12 @Html.Raw(t.S3PreSignedUrl.IsNullOrWhiteSpace() ? "disabled" : "")" target="_blank" data-container="body" data-toggle="tooltip" title="@t.OriginalFilename" type="@t.MimeType" download>
                    <span class="fa fa-cloud-download fs12"></span>
                </a>
            </td>

当前,当您单击它时,将打开另一个浏览器窗口,并开始使用Modal自动播放:

<div id="js_PlayRecordingPopup" class="popup-basic mfp-with-anim modalPopup">
    <div class="panel">
        <div class="admin-form">
            <div class="panel-heading">
                <span class="panel-title">
                    <i class="fa fa-play"></i> Play Recording
                </span>
            </div>
        </div>

        <div class="panel-body bt0 text-center p25">

            <p class="popupInfo fs12 mb5">Playing: <b class="text-info js_playingTitle"></b></p>
            <p class="popupInfo fs12">Filename: <b class="text-info js_playingFileName"></b></p>

            <div class="summaryBox popupSummary text-center audioContainerBox">
                <audio controls controlsList="nodownload" id="audRecording">
                    Your browser does not support the audio element.
                </audio>
            </div>

        </div>

        <div class="panel-footer">
            <div class="text-center">
                <input type="button" class="btn btn-primary" value="Done" data-bind="click: function(){ var sound = document.getElementById('audRecording'); if(sound != undefined) { sound.pause(); sound.currentTime = 0; } $.magnificPopup.close(); }">
            </div>
        </div>
    </div>
    <button title="Close (Esc)" type="button" class="mfp-close" data-bind="click: function(){ var sound = document.getElementById('audRecording'); if(sound != undefined) { sound.pause(); sound.currentTime = 0; }}">×</button>
</div>

有没有一种设置方式,如果我单击下载,它将立即下载文件?

这是源代码中的样子:

<td>
                    <a href="https:url/Audio/Recordings/TES/39e7ca51-1ac8-f395-3ae6-ff814dbde6c3/39e7ca51-e77c-65f1-c88e-47fe20f67ee1/o_1cj5s0tntp3aa1011o51tlrd8ba.mp3?X-Amz-Expires=86400&amp;response-content-disposition=inline%3B%20filename%3Drain-01.mp3&amp;X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=AKIAIOHIWJAIQSFECYZQ/20180724/eu-west-1/s3/aws4_request&amp;X-Amz-Date=20180724T104420Z&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Signature=02a5febff28eed31646a37fea0b8da7d7bcf4b0ffe9a3365d31a0ac3f0b2cabb" class="js_recordingDownloadButton document-link btn btn-info btn-block br2 btn-xs fs12 " target="_blank" data-container="body" data-toggle="tooltip" title="rain-01.mp3" type="audio/mp3" download>
                        <span class="fa fa-cloud-download fs12"></span>
                    </a>
                </td>

1 个答案:

答案 0 :(得分:1)

  1. 您可以在对Content-Type的回复中更改Content-Type: application/octet-stream。同时还要设置Content-Disposition: attachment; filename="filename.mp3"。确保文件名使用RFC 5987

  2. 中定义的编码
  3. 我看到您已经在HTML5上找到了download属性,但是您没有提供文件名。应该这样使用:

<a href="pathtofile.mp3" download="filename">

  1. 您可以随时测试dandavis编写的download.js。如果可行,您可以反转他的代码。