Chrome应用访问外部资源的JavaScript / jQuery的/ AngularJS [英] Chrome app access external resources with JavaScript / jQuery / AngularJS

查看:225
本文介绍了Chrome应用访问外部资源的JavaScript / jQuery的/ AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Chrome应用,可可视化的一些数据。我的数据被单独收集并存储在本地文件夹是无关的Chrome应用。我采用了棱角分明+ D3,我想我的应用程序从一个指定的文件夹获取数据。我该怎么做?

I am building a Chrome app that visualizes some data. My data is collected separately and is stored in a local folder that is unrelated to the Chrome app. I am using Angular + D3, and I want my app to grab the data from a specified folder. How can I do that?

目前,我试图使用 $ HTTP 服务,但它不工作,他们的方式我想要的。这里是code:

Currently, I am trying to use the $http service, but it doesn't work they way I want. Here is the code:

var app = angular.module('myApp', []);

app.controller('dataController', ['$scope', '$http', function($scope, $http) {
  $http.get('../data/file.json').success(function(data) {
    $scope.data = data;
    console.log($scope.data);
  });
}]);

问题是,在这种情况下,路径 ../数据/ file.json 只能导致应用程序的文件夹,里面就是一个文件,我不能指定在我的文件系统中任意目录/文件。我怎么能去呢?一般情况下,我可以有资源的外部文件夹Chrome应用可以使用(只读足以让我)?

The problem is that in this case the path ../data/file.json can only lead to a file inside the application's folder, i.e., I cannot specify an arbitrary directory/file on my filesystem. How can I go about this? In general, can I have an external folder with resources that a Chrome app can use (read only is enough for me)?

推荐答案

要获得一个任意目录下,你需要使用的 fileSystem.chooseEntry 功能,它会提示用户选择一个目录,然后返回的句柄你。下面是一个例子:

To gain access to an arbitrary directory, you need to use the fileSystem.chooseEntry function, which will prompt the user to select a directory, and then return a handle to you. Here is an example:

您必须先添加的必要权限清单文件:

You must first add the necessary permissions to your manifest file:

"permissions": [
      { "fileSystem": 
       ["directory", "retainEntries"] 
      }
  ]

要读取一个文件:

chrome.fileSystem.chooseEntry({ type: 'openDirectory' }, function (dirEntry) {
    dirEntry.getFile('file.json', {}, function (fileEntry) {
        fileEntry.file(function (file) {
            var reader = new FileReader();

            reader.onloadend = function (e) {
                var result = JSON.parse(this.result);
                console.log(result);
            };

            reader.readAsText(file);
        });
    });
});

要保留在会话目录的访问权限,你可以使用 retainEntry 获得id,你可以保存,后来与 restoreEntry 使用重新进入到目录。这样,用户只需要选择一次目录

To retain your access to a directory across sessions, you can use retainEntry to get an id which you can save, and later use with restoreEntry to regain access to the directory. This way the user only has to select the directory once.

这篇关于Chrome应用访问外部资源的JavaScript / jQuery的/ AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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