1

I have various types of files on my site the user can download. All files download like a charm except media files like mp3, mp4 etc. When the user clicks on mp3 etc downloads, a new tab opens and plays the media file instead of showing the download popup... Any ideas?

Apologies that I couldn't find exact issue on the net... All other answers shows the question the other way around.

function get_dl(e){

    var x = $('[name='+ e +']').attr('value');

 //sample code from w3schools
  if (window.XMLHttpRequest) {

    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
    window.open(this.responseText, '_blank');
            //This line is where I'm stuck

    }
  }
  xmlhttp.open("GET","getfile.php?q=" + x,true);
  xmlhttp.send();

}

All other files show's the download popup, except media files, they auto-play instead of downloading...

Kayaman
  • 72,141
  • 5
  • 83
  • 121
Chris
  • 27
  • 7
  • Each browser may be different for each user. Browsers will play files if the `Content-Type` header is recognized as a file that the browser can run for you. On the server, return the `Content-Type` header to the media type `application/octet-stream` and this will treat any file as a download instead of trying to run it. Meaning do this in `getfile.php`. Also refer to MIME type. -> https://stackoverflow.com/questions/152006/change-mime-type-of-output-in-php – daddygames Oct 03 '19 at 13:18

1 Answers1

0

Refering to Using XMLHttpRequest

Section: Handling binary data

function get_dl(e){

    var x = $('[name='+ e +']').attr('value');

 //sample code from w3schools
  if (window.XMLHttpRequest) {

    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
    window.open(this.responseText, '_blank');
        //This line is where I'm stuck

    }
  }
  //Add these two lines      
  var arraybuffer = xmlhttp.response;
  xmlhttp.responseType = arraybuffer;

  xmlhttp.open("GET","getfile.php?q=" + x,true);
  xmlhttp.send();

}

This worked for MIDI and MP3 media for me. Anybody else that can confirm if it worked for them?

Chris
  • 27
  • 7