Upload an audio file to the html anchor link, regardless of browser action

I have a link on a page that points to an MP3 file. I want to download this file using Right-click + Save Target As..and also with one click on the link .

I am currently facing a problem when different browsers have different behavior. IE plays the file in WMP, Firefox requests a download window, and Chrome starts playing the inline file. The expected behavior on a single click is that the browser should either download the file or give me a download dialog.

I tried the following:

  • Creating a hidden page iframeon the page and providing it as a target for my link, also tried to create a form with the desired location of the MP3 file as its action inside this iframeand send the form to the link link event
  • window.location = MP3 URLClick link setting
  • I tried several combinations window.open

None of them could help me. I just can't get the download dialog from the link.

An invisible binding link looks like this:

<a id="someID" href="myMP3file" target="whatever" title="Download" class="someClass">Download Me!</a>
+3
source share
2 answers

@CrabBucket - !! :) , ( ). HTTP , ;) -

{
    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(FileURL);
    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

    Stream myStream = myResponse.GetResponseStream();

    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=\"SaveMe.MP3\"");

    var readBytes = 10000000;
    var myAudResponse = new byte[readBytes];
    int fileLength;
    do {
        fileLength = myStream.Read(myAudResponse, 0, (int)readBytes);
        Response.OutputStream.Write(myAudResponse, 0, fileLength);
        Response.Flush();
    } while (fileLength > 0);
    myStream.Close();

    return new EmptyResult();
}

Try-Catch .. :) -, CrabBucket ( SO, , , !), !


! . , !

+1

. , http- (.ashx) - .

-

<a href="MyMp3Handler.ashx?fileName=MyFile">Click here to download</a>

.

, ( SO)

public void ProcessRequest (HttpContext context) {

    var filePath = Server.MapPath(context.Request.QueryString["fileName"].ToString()) + ".mp3";          
    if (!File.Exists(filePath))             
        return;          

    var fileInfo = new System.IO.FileInfo(filePath);         
    Response.ContentType = "application/octet-stream";         
    Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));         
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());         
    Response.WriteFile(filePath);         
    Response.End();
}

web.config.

, - , . , , .

, , application/octet-stream, , , . , - , PHP, mp3 , Firefox, IE Chrome, . asp.net, .

+1

All Articles