[英]File API not working with Blob in Google Chrome Extension
I'm writing an extension for Google Chrome where I need to download the images of a page and save them on disk on temporary files. 我正在编写Google Chrome浏览器扩展程序,需要在其中下载页面图像并将其保存在磁盘上的临时文件中。 To do this, I make an XMLHttpRequest and use it to download the image.
为此,我创建一个XMLHttpRequest并使用它来下载图像。 In the callback function, when I have the image in request.response, I request a temporary file from Windows using
window.webkitRequestFileSystem(window.TEMPORARY, 1024 * 1024 * 20, writeImageToFile, errorHandler)
. 在回调函数中,当图像包含在request.response中时,我使用
window.webkitRequestFileSystem(window.TEMPORARY, 1024 * 1024 * 20, writeImageToFile, errorHandler)
从Windows请求一个临时文件。 In the callback function writeImageToFile, I create the FileWriter object and write the blob into it. 在回调函数writeImageToFile中,我创建FileWriter对象并将blob写入其中。 The file is getting created but I'm not able to find out where it is being created.
该文件正在创建中,但是我无法找到它的创建位置。 I need the windows path of the file for the extension.
我需要扩展名文件的Windows路径。 Please help!
请帮忙!
For your purpose, you do not need a virtual file system. 为了您的目的,您不需要虚拟文件系统。 Use the
webkitURL.createObjectURL
method to create a URL from a blob: 使用
webkitURL.createObjectURL
方法从Blob创建URL:
window.webkitURL.createObjectURL(blob);
Blob
object using responseType
: responseType
直接获取Blob
对象: var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
// xhr.response is a Blob
var url = webkitURL.createObjectURL(xhr.response);
console.log('URL: ', url);
};
xhr.open('GET', 'http://example.com/favicon.ico');
xhr.send();
ArrayBuffer
response, and convert it to a blob ( demo ): ArrayBuffer
响应,并将其转换为blob( demo ): var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
// Create blob from arraybuffer
var bb = new (window.BlobBuilder || window.WebKitBlobBuilder)();
bb.append(xhr.response);
var url = webkitURL.createObjectURL(bb.getBlob());
console.log('URL: ', url);
};
xhr.open('GET', 'http://example.com/favicon.ico');
xhr.send();
(Use window.URL
and MozBlobBuilder
if you want to use the same code for Firefox). (如果要对Firefox使用相同的代码,请使用
window.URL
和MozBlobBuilder
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.