如何XHR本地文件?

时间:2014-08-30 18:06:49

标签: firefox-addon

通过给出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} = 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);
}

2 个答案:

答案 0 :(得分:2)

本地文件的XHR将导致status 0.这是正常的,并不意味着有错误。
XMLHttpRequest status是指实际的HTTP服务器响应,而不是访问本地文件的情况,因此将0作为status传递。

来自:How to convert an overlay extension to restartless

  

注意:使用XMLHttpRequest访问文件时:// URL   request.status未正确设置为200表示成功。在这样的   例如,request.readyState == 4,request.status == 0和   request.response将评估为true。

<强>更新

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

示例来自: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);

我在自己的附加组件中使用了上面的修改版本。

答案 1 :(得分:0)

感谢@erosman,本地文件的XHR始终返回状态0,但其中有数据。所以我修改了我的xhr函数来检查加载,如果url的方案是文件uri,如果它的文件是uri而不是它接受请求。

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, 'mdn.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));
        Services.ww.activeWindow.alert('ev.type=' + ev.type);
        switch (ev.type) {
            case 'load':
                if (xhr.status == 200) {
                    cb(xhr.response);
                    break;
                } else if (xhr.status == 0) {
                    var uritest = Services.io.newURI(url, null, null);
                    if (uritest.schemeIs("file")) {
                        //http://stackoverflow.com/a/25585661/1828637
                        //xhr'ing file uri always returns status 0 so this is not a real error
                        //so lets call cb and break
                        cb(xhr.response);
                        break;
                    } else {
                        //dont break so it goes into default error report
                        console.log('uri scheme is not file so it was real error, scheme was: ', uritest.scheme);
                    }
                }
            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);
}