Forum Discussion

Keith G.9's avatar
Keith G.9
New member | Level 1
11 years ago

Saving Data Directly from AJAX to Dropbox

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

Replies have been turned off for this discussion
  • Keith G.9's avatar
    Keith G.9
    New member | Level 1
    11 years ago

    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);
            }
        });
    });
    
  • Keith G.9's avatar
    Keith G.9
    New member | Level 1
    11 years ago

    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.

  • Werner S.6's avatar
    Werner S.6
    New member | Level 1
    10 years ago

    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

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    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.

  • Werner S.6's avatar
    Werner S.6
    New member | Level 1
    10 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!