angularjs - 如何以 Angular 下载谷歌驱动器文件

标签 angularjs google-drive-api

我有一个 Angular 应用程序,它使用谷歌驱动器选择器来允许用户选择要下载的文件。在谷歌开发人员凭证区域中,我将 javascript 起源和引荐来源设置为使用 localhost:3000,因此选择器可以正常打开。但是,从那时起,我似乎无法获取所选文件。这是尝试下载文件的 Angular 代码。

$scope.$watchCollection('googleFiles', function (newGoogleFiles) {
    if (newGoogleFiles.length > 0) {
        $scope.filename = newGoogleFiles[newGoogleFiles.length - 1].name;
        $scope.fileUrl = newGoogleFiles[newGoogleFiles.length - 1].url;
        var deferred = $q.defer();

        $http.get($scope.fileUrl, { responseType: 'arraybuffer'})
            .success(function (data) {
                console.log(data);
                deferred.resolve(data);
            });
        return deferred.promise;
    }
});

我得到的不是文件,而是 - XMLHttpRequest 无法加载 https://www.google.com/a/##############/edit?usp%3Ddrive_web&ltmpl=docs 。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问源“null”。

我猜我需要在文件的 $http.get 请求中设置一些内容,但我不知道是什么。有人有什么想法吗?谢谢。

最佳答案

您正在处理的是“CORS”(跨源资源共享)。

“Access-Control-Allow-Origin” header 必须在服务器上设置。您可以登录谷歌应用程序设置并告诉它它可以期待什么“来源”(例如:localhost,www.yourwebsite.com)。

关于angularjs - 如何以 Angular 下载谷歌驱动器文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104072/

相关文章:

javascript - 将已解决的 promise 分配给变量

javascript - Google Realtime 未记录的功能 : 'toJson' ?

python - 谷歌云端硬盘 SDK : How do you copy a file so the copy lives only in the root folder?

javascript - 使用 ANGULARJS 表单验证而不是 jquery 表单验证插件有什么好处?

javascript - 如何使用 jquery 在 Angular 中选择具有 ng-class attr 的元素

javascript - 如何处理来自复杂 Angular.js Web 表单的提交数据

java - 谷歌云端硬盘 API : Change ownership on file not working (only writer/reader works)

r - 使用 google drive R 包获取文件的大小

swift 。无法使用 Google Drive API 从个人 Google Drive 接收信息

java - 如何使用 @ControllerAdvice 在 Spring MVC 中返回 ResponseEntity 的方法中返回错误消息