cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done here!

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Saving Data Directly from AJAX to Dropbox

Saving Data Directly from AJAX to Dropbox

Keith G.9
New member | Level 1

I'm trying to avoid having to create static files. I would like to submit data directly to Dropbox with AJAX.

I tried one method of adding post data to the options to the url which returns the content. This doesn't work. I didn't expect it to work, but it did get far enough to time out while sending. Perhaps a problem with headers.

The other method would be not sending a url but just supply a data stream of text but the drop-in API doesn't support this.

<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="keywords.csv"');
if (isset($_POST['csv'])) {
$csv = $_POST['csv'];
echo $csv;
}

jQuery('#kwl-export-dropbox').click(function(e) {
    e.preventDefault();

    var kwl_url = jQuery('#kwl-url').val() + 'kwl-cvs.php';
    var results = jQuery('#kwl-results').val();
    var post_data = { cvs: results };

    var options = {
        data: post_data,
        type: 'POST',

        success: function () {
            console.log("Success! File saved to your Dropbox.");
        },

        progress: function(progress) {
            console.log("Progress: " + progress);
        },

        cancel: function() {
            console.log("Cancelled");
        },

        error: function(errorMessage) {
            console.log("Error: " + errorMessage);
            console.log("Data: " + results);
            console.log("URL: " + kwl_url);
        }
    };
    Dropbox.save(kwl_url, "keywords.csv", options); 
});
6 Replies 6

Re: Saving Data Directly from AJAX to Dropbox

Keith G.9
New member | Level 1

I have tried this as well. I get back "unauthorized". Not clear on the authorization process of using the API directly.

jQuery('#kwl-export-dropbox').click(function(e) {
    e.preventDefault();

    var dropbox_url = 'https://api-content.dropbox.com/1/files_put/auto/keywords.csv';
    var results = jQuery('#kwl-results').val();

    jQuery.ajax({
        url: dropbox_url,
        data: results,
        type: 'PUT',
        dataType : 'text',

        success: function(text) {
            console.log("SUCCESS");
            console.log("status: " + text);
        },

        error: function(xhr, status, errorThrown) {
            console.log("ERROR");
            console.log("status: " + status);
            console.log("errorThrown: " + errorThrown);
        },

        complete: function(xhr, status) {
            console.log("COMPLETE");
            console.log("status: " + status);
        }
    });
});

Re: Saving Data Directly from AJAX to Dropbox

Steve M.
Dropboxer

Take a look at https://blogs.dropbox.com/developers/2013/12/writing-a-file-with-the-dropbox-javascript-sdk/ and see if that helps. You'll need to use OAuth to authenticate the user. (The sample code in that blog post takes care of that.)

Re: Saving Data Directly from AJAX to Dropbox

Keith G.9
New member | Level 1

Thank you. The unofficial Javascript SDK has better documentation and a "Hello World" example. I now see that the official Javascript SDK for the Datastore API would provide the same functionality. It beats writing a 3-stage AJAX query to create, upload, and delete a static temporary file.

Re: Saving Data Directly from AJAX to Dropbox

Werner S.6
New member | Level 1

Works very well on Browser but how can i reach to get it work on android with the same lib: dropbox-datastores-1.2.0.js

Thx for advice

Re: Saving Data Directly from AJAX to Dropbox

Greg-DB
Dropboxer

Werner, the .js library you mentioned was only built for JavaScript. (Also, note that the Datastore API is deprecated.) For Android, you'd likely be better served by one of the Android/Java SDKs. For example for API v1 a.k.a. the Core API, there's an official SDK written in Java for Android here:

https://www.dropbox.com/developers-v1/core/sdks/android

Or for API v2, there's a Java SDK that supports Android here:

https://www.dropbox.com/developers/documentation/java

(Unless you mean you want it to work in the browser on Android, in which case the same JavaScript library should work like in any other supported browser.)

If you have any specific questions, feel free to open a new thread with the specifics.

Re: Saving Data Directly from AJAX to Dropbox

Werner S.6
New member | Level 1

THX Gregory, Android SDK would be a good solution. But i want to do an hybrid app with html5 an javascript, which "should" work on most mobile plattforms like android, wp8 and ios ... and acutally i am not so an insider in phonegap. But maybe i have to dive in phonegap ... Neverthelless thank you for the info.

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Werner S.6 New member | Level 1
  • User avatar
    Greg-DB Dropboxer
  • User avatar
    Keith G.9 New member | Level 1
  • User avatar
    Steve M. Dropboxer
What do Dropbox user levels mean?
Need more support?