本文介绍了如何使用OS.File将图像下载到桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个位于网络上的图像。例如: https://www.gravatar.com/avatar/eb9895ade1bd6627e054429d1e18b576?s=24&d=identicon&r=PG&f=1 我想下载这个文件到我的硬盘上。 我尝试了这个,但它没有工作:在这里我XHR它并请求数据类型arraybuffer,然后当我尝试写入OS.File我得到这个错误: TypeError:Value [对象ArrayBuffer]不能转换为一个指针osfile_shared_allthreads.jsm:443 var {Cu:utils,Cc:classes,Ci:instances} =组件; Cu.import('resource://gre/modules/Services.jsm'); 函数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; 默认: 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; //不设置它,所以它返回字符串,你不想要arraybuffer。你只需要这个,如果你的网址是一个zip文件或者一些你想下载的文件,并且把一个nsIArrayBufferInputStream从它或者某个地方取出来的话 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('成功保存的图像到桌面') $ b($) alert('将图像保存到桌面失败')} ); }); 解决方案 href =https://stackoverflow.com/a/25112976/3791822> https://stackoverflow.com/a/25112976/3791822 而另一半来自@ nmaier的解决方案在这里: https://stackoverflow.com/a/25148685/3791822 非常好;) 哈哈无论如何,你真的很接近。你得到了 ArrayBuffer ,但是把它作为 Uint8Array 传递给它: I have an image located on the web. For example: https://www.gravatar.com/avatar/eb9895ade1bd6627e054429d1e18b576?s=24&d=identicon&r=PG&f=1I would like to download this to a folder on my hd.I tried this but it didnt work: Here I XHR it and request datatype arraybuffer, then when i try to write it with OS.File I get this error: TypeError: Value [object ArrayBuffer] cannot be converted to a pointer osfile_shared_allthreads.jsm:443var {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') } );}); 解决方案 You stole half that from my solution here: https://stackoverflow.com/a/25112976/3791822And the other half from @nmaier's solution here: https://stackoverflow.com/a/25148685/3791822Very nice ;)Haha anyways you are real close. You got the ArrayBuffer but pass it as Uint8Array so:var promised = OS.File.writeAtomic(file, new Uint8Array(data));I don't know if this is the best way to download it. But it looks almost 100% async. Maybe it is the best way. Real cool man! 这篇关于如何使用OS.File将图像下载到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!