Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PhantomJS无法打开扩展名未知的本地文件_Javascript_Phantomjs - Fatal编程技术网

Javascript PhantomJS无法打开扩展名未知的本地文件

Javascript PhantomJS无法打开扩展名未知的本地文件,javascript,phantomjs,Javascript,Phantomjs,我正在使用phantomjs获取本地文件的屏幕截图。 现在我正在传递一个完全有效的html文件: <!DOCTYPE html><html><head><title>Title of the document</title></head><body>The file name dummy</body></html> js很简单: var page = require('webpage'

我正在使用phantomjs获取本地文件的屏幕截图。 现在我正在传递一个完全有效的html文件:

<!DOCTYPE html><html><head><title>Title of the document</title></head><body>The file name dummy</body></html> 
js很简单:

var page = require('webpage').create();
var system = require('system');

if (system.args.length !== 3) {
    console.log('Usage: script.js <URL> <screenshot destination>');
    phantom.exit();
}

page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};

page.open(system.args[1], function(status) {
    if (status !== 'success') {
        console.log('Failed to load address '+system.args[1]+' ' + page.reason_url               + ": " + page.reason);
        phantom.exit(-1);
    }
    page.render(system.args[2]);
    phantom.exit();
});
我在这里使用了详细错误输出的链接:

这就是结果:

= onNavigationRequested
  destination_url: file:///D:/dummy.hoo
  type (cause): Other
  will navigate: true
  from page's main frame: true
= onResourceRequested()
  request: {
    "headers": [
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.0 Safari/534.34"
        },
        {
            "name": "Accept",
            "value": "*/*"
        }
    ],
    "id": 1,
    "method": "GET",
    "time": "2015-03-01T16:40:11.080Z",
    "url": "file:///D:/dummy.hoo"
}
= onLoadStarted()
  leaving url: about:blank
= onResourceReceived()
  id: 1, stage: "start", response: {"bodySize":110,"contentType":null,"headers":[{"name":"Last-Modified","value":"Sun, 01 Mar 2015 17:13:02 GMT"},{"name":"Content-Length","value":"110"}],"id":1,"redir
ectURL":null,"stage":"start","status":null,"statusText":null,"time":"2015-03-01T16:40:11.082Z","url":"file:///D:/dummy.hoo"}
= onResourceReceived()
  id: 1, stage: "end", response: {"contentType":null,"headers":[{"name":"Last-Modified","value":"Sun, 01 Mar 2015 17:13:02 GMT"},{"name":"Content-Length","value":"110"}],"id":1,"redirectURL":null,"sta
ge":"end","status":null,"statusText":null,"time":"2015-03-01T16:40:11.082Z","url":"file:///D:/dummy.hoo"}
= onLoadFinished()
  status: fail
Failed to load address file:///D:/dummy.hoo undefined: undefined

我能够在phantomjs中找到处理mime类型的代码(不同驱动程序的多个位置):

等等

本地文件背后的要点(hehe)不发送包含MIME类型的头信息。因此,Phantomjs不知道应该调用哪个处理程序来正确呈现内容。我基本上可以将.jpeg重命名为.exe,只要web服务器发送jpg mime类型,它就会正确呈现。这是web中常见的行为,基于任何内容(正则表达式、扩展名等)重定向url部分

Phantom没有检测文件真实内容的某种推理(这是完全合理的),因此它必须依赖于给定的文件扩展名和映射


因此,我知道我必须接受我可以使用
html
htm
扩展来呈现html数据,而不是其他任何内容。

检查路径
file:///D:/TestData/dummy.hoo
file:///D:/dummy.hoo
?它没有文档记录,但我在代码中看到了一个构造,它通过查看文件扩展名来决定特定文件的处理方式。我现在在源代码中找不到那个部分。@rel打字错误,我已经解决了。你的问题还没有解决吗?@KiranRS没有,那是一个打字错误。这个问题仍然存在。
Failed to load address file:///D:/dummy.hoo undefined: undefined
= onNavigationRequested
  destination_url: file:///D:/dummy.hoo
  type (cause): Other
  will navigate: true
  from page's main frame: true
= onResourceRequested()
  request: {
    "headers": [
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.0 Safari/534.34"
        },
        {
            "name": "Accept",
            "value": "*/*"
        }
    ],
    "id": 1,
    "method": "GET",
    "time": "2015-03-01T16:40:11.080Z",
    "url": "file:///D:/dummy.hoo"
}
= onLoadStarted()
  leaving url: about:blank
= onResourceReceived()
  id: 1, stage: "start", response: {"bodySize":110,"contentType":null,"headers":[{"name":"Last-Modified","value":"Sun, 01 Mar 2015 17:13:02 GMT"},{"name":"Content-Length","value":"110"}],"id":1,"redir
ectURL":null,"stage":"start","status":null,"statusText":null,"time":"2015-03-01T16:40:11.082Z","url":"file:///D:/dummy.hoo"}
= onResourceReceived()
  id: 1, stage: "end", response: {"contentType":null,"headers":[{"name":"Last-Modified","value":"Sun, 01 Mar 2015 17:13:02 GMT"},{"name":"Content-Length","value":"110"}],"id":1,"redirectURL":null,"sta
ge":"end","status":null,"statusText":null,"time":"2015-03-01T16:40:11.082Z","url":"file:///D:/dummy.hoo"}
= onLoadFinished()
  status: fail
Failed to load address file:///D:/dummy.hoo undefined: undefined