如何XHR本地文件? [英] How to XHR local files?

查看:209
本文介绍了如何XHR本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个失败的结果是0而不是200.我确定这与xmlhttprequests不允许访问本地驱动器有关,但是这个应该只适用于web范围,而不是xpcom范围吧?

  var pathh = OS.Path.toFileURI(OS.Path.join(OS.Constants.Path.desktopDir,'test.png')); 
xhr(pathh,data => {
Services.prompt.alert(null,'XHR Success',data);
});

完整代码

  var {Cu:utils,Cc:classes,Ci:instances} =组件; 
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/osfile.jsm');

var pathh = OS.Path.toFileURI(OS.Path.join(OS.Constants.Path.desktopDir,'test.png'));
xhr(pathh,data => {
Services.prompt.alert(null,'XHR Success',data);
});


$ b / **** my xhr func **** /
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;

默认:
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);

$ / code $ / pre

解决方案

状态 0。这是正常的,并不意味着有错误。

XMLHttpRequest status 指的是实际的HTTP服务器响应,而不是在访问本地文件的情况下,所以传递一个0作为 status



发件人:如何将覆盖扩展转换为无法重启注意:当使用XMLHttpRequest来访问文件:// URL $ b时,
$ b


$ b request.status未正确设置为200来表示成功。在这样的
的情况下,request.readyState == 4,request.status == 0和
request.response将评估为true。


更新:



个人而言,我会使用API​​,而不是打扰 status



示例来自:

  let url =http:// www。 example.com/; 
let request = Components.classes [@ mozilla.org/xmlextras/xmlhttprequest; 1]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
request.onload = function(aEvent){
//成功完成的代码
//window.alert(\"Response Text:+ aEvent.target.responseText);
};
request.onerror = function(aEvent){
//出现问题,处理错误
//window.alert(\"Error Status:+ aEvent.target.status);
};
request.open(GET,url,true);
request.send(null);

我在自己的加载项中使用上述修改后的版本。 b

This fails by giving result of 0 instead of 200. Im sure this has to do with xmlhttprequests being not allowed to access local drive but that should only apply to web scope, not xpcom scope right?

var pathh = OS.Path.toFileURI(OS.Path.join(OS.Constants.Path.desktopDir, 'test.png'));
xhr(pathh, data => {
    Services.prompt.alert(null, 'XHR Success', data);
});

full code below

var {Cu: utils, Cc: classes, Ci: instances} = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/osfile.jsm');

var pathh = OS.Path.toFileURI(OS.Path.join(OS.Constants.Path.desktopDir, 'test.png'));
xhr(pathh, data => {
    Services.prompt.alert(null, 'XHR Success', data);
});



/****my xhr func****/
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 for local files will result in status 0. That is normal and doesn't mean there was an error.
XMLHttpRequest status refers to actual HTTP server response which is not the case in accessing local files, therefore a 0 is passed as status.

From: How to convert an overlay extension to restartless

Note: When using XMLHttpRequest to access a file:// URL the request.status is not properly set to 200 to indicate success. In such cases, request.readyState == 4, request.status == 0 and request.response will evaluate to true.

Update:

Personally I would use the API and not bother with status

Example from: Connecting to Remote Content

let url = "http://www.example.com/";
let request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
              .createInstance(Components.interfaces.nsIXMLHttpRequest);
request.onload = function(aEvent) {
  // code to do on success
  //window.alert("Response Text: " + aEvent.target.responseText);
};
request.onerror = function(aEvent) {
  // there was a problem, handle error
  //window.alert("Error Status: " + aEvent.target.status);
};
request.open("GET", url, true);
request.send(null);

I use a modified version of above in my own add-ons.

这篇关于如何XHR本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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