【问题标题】:local file system access - solutions?本地文件系统访问 - 解决方案?
【发布时间】:2015-05-11 09:31:53
【问题描述】:

我发布的第二个问题。对网络编程还是很陌生,所以请原谅我的无知。

我有一个基于网络的 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>

谢谢

【问题讨论】:

  • 您能否分享您基于 Web 的 javascript,它可以使用对 FS 具有完全访问权限的 HTA / JScript 实现,或者转换并直接放入您的 VBA。

标签: javascript vba file system local


【解决方案1】:

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

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

【讨论】:

  • 是的好主意。我可以知道该文件是否存在,因为所有文件都重命名为包含后缀#FHM#,因此易于测试。所以我会检查带有 #FHM# 的文件的标准下载位置,如果没有,请进行搜索 - 非常感谢
  • 我很高兴能帮上忙
猜你喜欢
  • 2020-08-01
  • 2013-06-28
  • 2016-11-22
  • 1970-01-01
  • 2015-03-29
  • 2016-01-01
  • 2016-09-18
  • 1970-01-01
  • 2021-05-30
相关资源
最近更新 更多