在 chrome 扩展中使用 html5 blob 下载 mp3 文件 [英] Downloading mp3 files using html5 blobs in a chrome-extension

查看:103
本文介绍了在 chrome 扩展中使用 html5 blob 下载 mp3 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以下载 mp3 文件的 google-chrome-extension.我正在尝试使用 HTML5 blob 和 iframe 来触发下载,但它似乎不起作用.这是我的代码:

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 be working. Here is my code:

var finalURL = "server1.example.com/u25561664/audio/120774.mp3";

var xhr = new XMLHttpRequest();
    xhr.open("GET", finalURL, true);
    xhr.setRequestHeader('Content-Type', 'application/octet-stream');
    xhr.onreadystatechange = function() 
    {
        if(xhr.readyState == 4 && xhr.status == 200) 
        {   
            var bb = new (window.BlobBuilder || window.WebKitBlobBuilder)();
            bb.append(xhr.responseText);
            var blob = bb.getBlob("application/octet-stream");

            var saveas = document.createElement("iframe");
            saveas.style.display = "none";

            saveas.src = window.webkitURL.createObjectURL(blob); 

            document.body.appendChild(saveas);

            delete xhr;
            delete blob;
            delete bb;
        }
    }
 xhr.send();

在控制台中查看时,blob 已正确创建,设置看起来正确:

When looked in the console, the blob is created correctly, the settings look right:

尺寸:15312172类型:应用程序/八位字节流"

size: 15312172 type: "application/octet-stream"

但是,当我尝试由 createObjectURL() 创建的链接时,

However, when I try the link created by the createObjectURL(),

blob:chrome-extension://dkhkkcnjlmfnnmaobedahgcljonancbe/b6c2e829-c811-4239-bd06-8506a67cab04

blob:chrome-extension://dkhkkcnjlmfnnmaobedahgcljonancbe/b6c2e829-c811-4239-bd06-8506a67cab04

我得到一个空白文档和一个警告说

I get a blank document and a warning saying

资源被解释为文档但使用 MIME 类型传输应用程序/八位字节流.

Resource interpreted as Document but transferred with MIME type application/octet-stream.

如何让我的代码正确下载文件?

How can get my code to download the file correctly?

推荐答案

以下代码在 Google chrome 14.0.835.163 中对我有用:

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);

这篇关于在 chrome 扩展中使用 html5 blob 下载 mp3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆