如何添加数据到动态数组和使用数组在dropbox保护程序api

How to append data to dynamic array and use the array in dropbox saver api?

本文关键字:数组 dropbox api 程序 保护 动态 何添加 添加 数据      更新时间:2023-09-26

我创建了几个按钮和按钮url和文件名的onclick发送到动态数组,当我点击dropbox保护程序按钮时,我希望保护程序功能使用该动态数组将文件发送到dropbox。我当前的代码给了我这个错误:丢失的文件。看到文档。

似乎我没有正确地保存数据到数组,我没有正确地传递它到保存函数。有人能帮我解决这个问题吗?由于

注意:我只是想使用我的动态数组,而不是硬编码的url和文件名,如下所示:

编辑:我添加选项。Files = Files;在调用dropbox之前。保存并声明我的数组外addtoarray函数。现在我得到这个错误:G是undefined at dropins.js(第1行)。

files: [
        {'url': '<?PHP  echo $imagePath1_Value; ?>', 'filename': '1.jpg'},
        {'url': '<?PHP  echo $imagePath2_Value; ?>', 'filename': '2.jpg'},
        {'url': '<?PHP  echo $imagePath3_Value; ?>', 'filename': '3.jpg'},
    ],
Dropbox Api Js:
<script type="text/javascript" src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="xxxxxxxxxxxxxx"></script>
<script>

保存fileurl和filename到数组函数:

 var i=1;
  files = new Array();   
    function addtoArray(a,b){
    alert("URL:"+a+"'nFileName:"+b);
    files[i] = { url: +a, filename: +b };
    i++;
    };

Dropbox saver功能:

function saver(){
var options = { 
//here i want to use array i created above instead of hardcode filepath and filenames

//files: [
        // You can specify up to 100 files.
        // ...
        //{'url': '<?PHP  echo $imagePath1_Value; ?>', 'filename': '1.jpg'},
        //{'url': '<?PHP  echo $imagePath2_Value; ?>', 'filename': '2.jpg'},
        //{'url': '<?PHP  echo $imagePath3_Value; ?>', 'filename': '3.jpg'},
    //],
    // Success is called once all files have been successfully added to the user's
    // Dropbox, although they may not have synced to the user's devices yet.
    success: function () {
        // Indicate to the user that the files have been saved.
        alert("Success! Files saved to your Dropbox.");
    },
    // Progress is called periodically to update the application on the progress
    // of the user's downloads. The value passed to this callback is a float
    // between 0 and 1. The progress callback is guaranteed to be called at least
    // once with the value 1.
    progress: function (progress) {},
    // Cancel is called if the user presses the Cancel button or closes the Saver.
    cancel: function () {},
    // Error is called in the event of an unexpected response from the server
    // hosting the files, such as not being able to find a file. This callback is
    // also called if there is an error on Dropbox or if the user is over quota.
    //error: function (errorMessage) {}
error:function (errorMessage) { alert("ERROR: " + errorMessage); }
};
options.files = files;
Dropbox.save(options);
};
</script>

html正文:

<body>
<button onclick="addtoArray('<?PHP  echo $imagePath1_Value; ?>','filename1.jpg')">add to array</button>
<button onclick="addtoArray('<?PHP  echo $imagePath2_Value; ?>','filename2.jpg')">add to array</button>
<button onclick="saver()">save</button>

你可以随心所欲地构建你的files数组,只要options.files数组在调用Dropbox.save时包含必要的信息。例如,当你调用addtoArray时,附加到一个共享的files变量(现在看起来你每次都在创建一个新的),然后在调用Dropbox.save之前设置options.files = files