gpt4 book ai didi

firefox-addon - 如何使用 OS.File 下载图像到桌面

转载 作者:行者123 更新时间:2023-12-02 01:46:22 25 4
gpt4 key购买 nike

我在网上有一张图片。例如:https://www.gravatar.com/avatar/eb9895ade1bd6627e054429d1e1​​8b576?s=24&d=identicon&r=PG&f=1

我想将其下载到我硬盘上的一个文件夹中。

我试过了,但没用:在这里我对它进行 XHR 并请求数据类型 arraybuffer,然后当我尝试用 OS.File 编写它时,我得到这个错误:TypeError: Value [object ArrayBuffer] cannot be converted to一个指针 osfile_shared_allthreads.jsm:443

var {Cu: utils, Cc: classes, Ci: instances} = Components;
Cu.import('resource://gre/modules/Services.jsm');
function xhr(url, cb) {
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);

let handler = ev => {
evf(m => xhr.removeEventListener(m, handler, !1));
switch (ev.type) {
case 'load':
if (xhr.status == 200) {
cb(xhr.response);
break;
}
default:
Services.prompt.alert(null, 'XHR Error', 'Error Fetching Package: ' + xhr.statusText + ' [' + ev.type + ':' + xhr.status + ']');
break;
}
};

let evf = f => ['load', 'error', 'abort'].forEach(f);
evf(m => xhr.addEventListener(m, handler, false));

xhr.mozBackgroundRequest = true;
xhr.open('GET', url, true);
xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS | Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_PERSISTENT_CACHING;
xhr.responseType = "arraybuffer"; //dont set it, so it returns string, you dont want arraybuffer. you only want this if your url is to a zip file or some file you want to download and make a nsIArrayBufferInputStream out of it or something
xhr.send(null);
}

xhr('https://www.gravatar.com/avatar/eb9895ade1bd6627e054429d1e18b576?s=24&d=identicon&r=PG&f=1', data => {
Services.prompt.alert(null, 'XHR Success', data);
var file = OS.Path.join(OS.Constants.Path.desktopDir, "test.png");
var promised = OS.File.writeAtomic(file, data);
promised.then(
function() {
alert('succesfully saved image to desktop')
},
function(ex) {
alert('FAILED in saving image to desktop')
}
);
});

最佳答案

你在这里从我的解决方案中偷走了一半:https://stackoverflow.com/a/25112976/3791822

另一半来自@nmaier 的解决方案:https://stackoverflow.com/a/25148685/3791822

非常好;)

哈哈,反正你们真的很亲近。您获得了 ArrayBuffer 但将其作为 Uint8Array 传递,因此:

var promised = OS.File.writeAtomic(file, new Uint8Array(data));

我不知道这是不是最好的下载方式。但它看起来几乎是 100% 异步的。也许这是最好的方法。真正的帅哥!

关于firefox-addon - 如何使用 OS.File 下载图像到桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com