Javascript 本地文件系统访问-解决方案?
我发的第二个问题。对网络编程还是很陌生的,请原谅我的无知 我有一个基于web的javascript,它可以访问用户的Gmail帐户,并将附件下载到Chrome中指定的本地下载文件夹 然后手动将这些文件传输到另一个目录,并由Excel VBA脚本处理这些文件 我希望能够跳过手动传输步骤,将文件直接保存到Excel正在查看的文件夹中。我可以使用Excel脚本来移动文件,但它只有在用户没有更改Chrome默认下载文件夹位置时才起作用,所以它不是万无一失的 我相信这在javascript中是不可能的,但在其他语言中可能吗?或者我需要一种完全不同的方法吗?如果其他语言可以,我需要看哪种语言和哪种方法 这是代码的下载部分,应用户的请求,它目前处于以下状态:Javascript 本地文件系统访问-解决方案?,javascript,vba,file,system,local,Javascript,Vba,File,System,Local,我发的第二个问题。对网络编程还是很陌生的,请原谅我的无知 我有一个基于web的javascript,它可以访问用户的Gmail帐户,并将附件下载到Chrome中指定的本地下载文件夹 然后手动将这些文件传输到另一个目录,并由Excel VBA脚本处理这些文件 我希望能够跳过手动传输步骤,将文件直接保存到Excel正在查看的文件夹中。我可以使用Excel脚本来移动文件,但它只有在用户没有更改Chrome默认下载文件夹位置时才起作用,所以它不是万无一失的 我相信这在javascript中是不可能的,但
<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>
谷歌硬盘文件下载过程:
var客户端ID='XXXXXXXXXX'//为了隐私而删除
var作用域https://www.googleapis.com/auth/drive';
/**
*在加载客户端库以启动身份验证流时调用。
*/
函数handleClientLoad(){
setTimeout(checkAuth,1);
}
/**
*检查当前用户是否已授权应用程序。
*/
函数checkAuth(){
gapi.auth.authorize({
“客户id”:客户id,
“范围”:范围,
“立即”:真
},
手工(结果);
}
/**
*当授权服务器答复时调用。
*
*/
函数handleAuthResult(authResult){
var authButton=document.getElementById('authorizeButton');
var filePicker=document.getElementById('filePicker');
authButton.style.display='none';
filePicker.style.display='none';
if(authResult&!authResult.error){
//已成功检索访问令牌,可以向API发送请求。
filePicker.style.display='block';
filePicker.onclick=downloadFile;//允许手动开始下载
setTimeout(downloadFile(),5000);
}否则{
//无法检索访问令牌,请显示按钮以启动授权流。
authButton.style.display='block';
authButton.onclick=函数(){
gapi.auth.authorize({
“客户id”:客户id,
“范围”:范围,
“立即”:false
},
手工(结果);
};
}
}
/**
*开始下载文件。
*
*
*/
函数下载文件(){
log(“调用驱动api”);
load('drive','v2',makeRequest);
}
函数makeRequest(){
控制台日志(“发出请求”);
var request=gapi.client.drive.files.list();
请求执行(功能(resp){
var x=[];//用于修改文件列表的数组,以仅包括不在垃圾箱中的文件和后缀为#FHM的文件#
对于(i=0;i下载“+(i+1)+”或“+x.length+”已完成。”;
}
});
//setTimeout(函数(){
//自我封闭;
//}, 5000);
}
函数下载url(url){
var iframe=document.createElement(“iframe”);
iframe.src=url;
iframe.style.display=“无”;
document.body.appendChild(iframe);
}
函数垃圾文件(id){
var requestTrash=gapi.client.drive.files.trash({
“文件id”:id
});
execute(函数(resp){});
}
谢谢您的代码可以尝试复制文件,如果不成功,请用户提供提取文件的正确路径。你是对的,这是Javascript无法完成的。如果希望避免向用户询问路径,可以实现一个模块来搜索文件
有没有办法知道文件是否被提取?如果是这样的话,您可以利用这些知识来了解缺少成功复制是否会触发文件搜索或文件路径请求。您是否可以共享基于web的javascript,它可以使用完全访问FS的HTA/JScript实现,或者转换并直接放入您的VBA中。好主意。我可以知道该文件是否存在,因为所有文件都重命名为包含后缀#FHM#,因此易于测试。因此,我将使用#FHM#检查文件的标准下载位置,如果没有,则进行搜索-非常感谢我很高兴能提供帮助