0

ユーザーが[音楽をダウンロード]リンクをクリックしたときに、次のようなポップアップを表示したいですか?

ここに画像の説明を入力してください

次のコードを使用すると、リンクをクリックすると、AppleRealTImeなどのブラウザに埋め込まれた音楽プレーヤーが開きます。

<a href="../mp3/horse.mp3" target="_blank">Click to download</a>

ブラウザが音楽を実行しないようにするにはどうすればよいですか(このプラグインを無効化/削除せずに)。

ブラウザで音楽をダウンロードし、再生しないようにします。

4

4 に答える 4

1

onclickまず、アンカーから属性を削除します。このためにJavaScriptを使用する必要はありません。

次に、mp3が要求されたときに、サーバーにcontent-disposition: attachmentHTTP応答ヘッダーを返すようにします。これをどのように行うかは、サーバーによって異なります。たとえば、Apacheでは、サーバー側のプログラミング言語を使用せずにHeaderディレクティブを使用できます。または、Javaの例をcontent-type参照してください(ただし、mp3には正しい設定をする必要があります)。

于 2013-01-20T14:42:29.723 に答える
1

nginxでは、これをnginx.confに追加できます

location ~* (.*\.mp3) {
    types { application/octet-stream .mp3; }
    default_type application/octet-stream;
}

そしてApacheでこれをhttpd.confまたは.htaccessに追加します

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

私はこれをhttp://mp3boo.ccconfigで使用し、mp3ファイルを強制的にダウンロードするために非常にうまく機能しました。hth

于 2015-08-13T00:05:29.483 に答える
0

サーバーの.htaccessファイルに次のコード行を追加して、サーバーから特定のファイルタイプを強制的にダウンロードできます。その後、通常のリンクが機能するはずです。

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>
于 2013-01-20T15:14:26.437 に答える
0
Explaining here how I did it:

1. Add following snippet in web.xml:



<pre><code>
<servlet>
      <servlet-name>MyDownloadServlet</servlet-name>
      <servlet-class>com.lokur.MyDownloadServlet</servlet-class>      
  </servlet>
  <servlet-mapping>
      <servlet-name>MyDownloadServlet</servlet-name>
      <url-pattern>*.download</url-pattern>
  </servlet-mapping>

</pre></code>

2. Add following in your servlet:



public class MyDownloadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse response)
            throws ServletException, IOException {


        //Set the headers.
        response.setContentType("application/x-download"); 
        response.setHeader("Content-Disposition", "attachment; filename=downloaded_horse.mp3");

    //TODO: pull the file path from the request parameters  
        InputStream fileIn = getServletContext().getResourceAsStream("mp3/horse.mp3");
        ServletOutputStream outstream = response.getOutputStream();

        byte[] outputByte = new byte[40096];

        while(fileIn.read(outputByte, 0, 40096) != -1)
        {
            outstream.write(outputByte, 0, 40096);
        }
        fileIn.close();
        outstream.flush();
        outstream.close();


    }


}


3. Finally this is the requested jsp:


<pre> <code>
       <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"

     %>  
    <body>  
    <form action="getMusicFile.download" method="post">
        Wanna download? <input type="submit" value="Download music"> 
      </form>  
    </body>



That's all to it!

Cheers,
Akshay :)
于 2013-01-20T16:07:52.487 に答える