如何将数据附加到动态数组并在dropbox saver api中使用该数组?

时间:2015-06-10 17:29:09

标签: javascript arrays dropbox dropbox-api

我创建了一些按钮,然后点击按钮url和文件名发送到动态数组,当我点击dropbox保护程序按钮时,我希望保护程序功能使用该动态数组将文件发送到dropbox。我当前的代码给了我这个错误:缺少文件。参见文档。

它似乎我没有正确地将数据保存到数组,我没有正确地将它传递给saver功能。任何人都可以帮我解决这个问题。谢谢

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

已编辑:我添加了options.files = files;在调用dropbox.save之前,在addtoarray函数之外声明我的数组。现在我收到此错误:在dropins.js(第1行)中未定义G.

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保护程序功能:

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>

1 个答案:

答案 0 :(得分:2)

您可以根据需要构建files数组,只要options.files数组在您调用Dropbox.save时包含必要的信息即可。例如,当您致电files时(现在看起来您每次都要制作一个新变量),请附加到共享的addtoArray变量,然后在调用options.files = files之前设置Dropbox.save