本地文件系统访问 - 解决方案?

时间:2015-05-11 09:31:54

标签: javascript vba file system local

我发布的第二个问题。对网络编程来说还是一个新手,所以请原谅我的无知。

我有一个基于网络的JavaScript,可以访问用户的Gmail帐户,并将附件下载到Chrome中指定的本地下载文件夹。

然后将这些文件手动传输到另一个目录,Excel VBA脚本处理这些文件。

我希望能够跳过手动传输步骤并将文件直接保存到Excel正在查看的文件夹中。我可以使用Excel脚本来移动文件,但只有在用户没有更改Chrome默认下载文件夹位置时它才有效,所以它不是万无一失。

我相信这对javascript来说是不可能的,但它是否可能与其他语言或我需要一个完全不同的方法?如果其他语言可以使用哪种方法,我需要查看哪种方法?

这是下面的用户OmegaStripes请求时代码的下载部分:

<html>

<head>Google Drive File Download Process:
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <script type="text/javascript">
        var CLIENT_ID = 'XXXXXXXXXXX';//removed for privacy
        var SCOPES = 'https://www.googleapis.com/auth/drive';

        /**
         * Called when the client library is loaded to start the auth flow.
         */

        function handleClientLoad() {
            window.setTimeout(checkAuth, 1);
        }

        /**
         * Check if the current user has authorized the application.
         */

        function checkAuth() {
            gapi.auth.authorize({
                    'client_id': CLIENT_ID,
                    'scope': SCOPES,
                    'immediate': true
                },
                handleAuthResult);
        }

        /**
         * Called when authorization server replies.
         *
         */

        function handleAuthResult(authResult) {
            var authButton = document.getElementById('authorizeButton');
            var filePicker = document.getElementById('filePicker');
            authButton.style.display = 'none';
            filePicker.style.display = 'none';
            if (authResult && !authResult.error) {
                // Access token has been successfully retrieved, requests can be sent to the API.
                filePicker.style.display = 'block';
                filePicker.onclick = downloadFile; // to allow for manual start of downloads
                window.setTimeout(downloadFile(), 5000);

            } else {
                // No access token could be retrieved, show the button to start the authorization flow.
                authButton.style.display = 'block';
                authButton.onclick = function() {
                    gapi.auth.authorize({
                            'client_id': CLIENT_ID,
                            'scope': SCOPES,
                            'immediate': false
                        },
                        handleAuthResult);
                };
            }
        }

        /**
         * Start the file download.
         *
         *
         */

        function downloadFile() {
            console.log("call drive api");
            gapi.client.load('drive', 'v2', makeRequest);
        }




        function makeRequest() {
            console.log("make request");
            var request = gapi.client.drive.files.list();
            request.execute(function(resp) {

                var x = []; //array for revised list of files to only include those not in the trash and those which have a suffix #FHM#
                for (i = 0; i < resp.items.length; i++) {
                    if (resp.items[i].labels.trashed != true && resp.items[i].title.substring(0, 5) == "#FHM#") {
                        x.push([resp.items[i].title, resp.items[i].webContentLink, resp.items[i].id]);
                    }
                }

                if (x.length == 0) {
                    document.getElementById("filePicker").value = "There are no files to download";
                }


                for (i = 0; i < x.length; i++) {
                    console.log(x.length);
                    var dlUrl = x[i][1];
                    fileIdentity = x[i][2];
                    downloadUrl(dlUrl);
                    trashFile(fileIdentity);

                    filePicker.style.display = 'none';
                    document.getElementById("bodyText").innerHTML = "<br>Download " + (i + 1) + " of " + x.length + " completed.";
                }


            });

            //window.setTimeout(function() {
            //    self.close;
            //}, 5000);

        }

        function downloadUrl(url) {
            var iframe = document.createElement("iframe");
            iframe.src = url;
            iframe.style.display = "none";
            document.body.appendChild(iframe);
        }

        function trashFile(id) {
            var requestTrash = gapi.client.drive.files.trash({
                    'fileId': id
                });
            requestTrash.execute(function(resp) {});
        }
    </script>
    <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</head>


<body>
    <!--Add buttons for the user to start the process -->
    <input type="button" id="filePicker" style="display: none" value="If download does not start after 5 seconds, click here" />
    <input type="button" id="authorizeButton" style="display: none" value="Authorize" />
     <b id="bodyText"></b>
</body>

感谢

1 个答案:

答案 0 :(得分:0)

您的代码可能会尝试复制文件,如果不成功,请让用户提供解压缩文件的正确路径。你是对的,Javascript无法完成。如果您想避免向用户询问路径,可以实现一个模块来搜索文件。

有没有办法可以知道文件是否被提取?如果是这样,您可以使用此知识来了解缺少成功复制是否应该触发文件搜索或文件路径请求。