Downloading mp3 files using html5 blobs in a chrome-extension

前端 未结 2 1309
醉梦人生
醉梦人生 2020-12-09 23:39

I am trying to create a google-chrome-extension that will download an mp3 file. I am trying to use HTML5 blobs and an iframe to trigger a download, but it doesn\'t seem to b

相关标签:
2条回答
  • 2020-12-10 00:00

    I'm not sure, but i think this is your server's trouble. I've just tried your piece of code to download some sample blob of mp3-file and everything went ok. So maybe:

    1. this file doesn't exist on your server
    2. you server outputs wrong mime type for mp3 files
    0 讨论(0)
  • 2020-12-10 00:07

    The below code worked for me in Google chrome 14.0.835.163:

    var finalURL = "http://localhost/Music/123a4.mp3";
    var xhr = new XMLHttpRequest();
    xhr.overrideMimeType("application/octet-stream");
    //xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.open("GET", finalURL, true);
    xhr.responseType = "arraybuffer";
    xhr.onload = function() {
          var bb = new (window.BlobBuilder || window.WebKitBlobBuilder)();
          var res = xhr.response;
          if (res){
              var byteArray = new Uint8Array(res);
          }
          bb.append(byteArray.buffer);
          var blob = bb.getBlob("application/octet-stream");
          var iframe = document.createElement("iframe");
          iframe.style.display = "none";
          iframe.src = window.webkitURL.createObjectURL(blob);
          document.body.appendChild(iframe);
    };
    xhr.send(null);
    
    0 讨论(0)
提交回复
热议问题