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

400 Views Asked by At

I created a few buttons and onlick of the button url and filename is send to dynamic array and when i click dropbox saver button i want the saver function uses that dynamic array to send the files to dropbox. My current code gives me this error:Missing files.See Documentation.

Its seems that i didnt save the data correctly to array and i didnt pass it correctly to saver funciton. could any one help me fix this problem.Thanks

Note: i just want to use my dynamic array instead of hardcoded url and filenames as shown here:

Edited: I add options.files = files; before calling dropbox.save and declared my array outside addtoarray function . Now i get this error:G is undefined at dropins.js(line 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>

Saveing fileurl and filename to array function:

 var i=1;
  files = new Array();   
    function addtoArray(a,b){
    alert("URL:"+a+"\nFileName:"+b);

    files[i] = { url: +a, filename: +b };
    i++;

    };

Dropbox savers function:

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>

Body of 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

There are 1 best solutions below

4
On

You can build your files array however you want, as long as options.files array contains the necessary information when you call Dropbox.save. E.g., append to a shared files variable when you call addtoArray (right now it looks like you are making a new one each time) and then set options.files = files before calling Dropbox.save.