How to get all files Save to Dropbox on my HTML page

I have saving images to Dropbox using Javascript like

document.forms.newsletter.addEventListener('submit', function 
     cb(evt) {
        //evt.preventDefault()

        // API key from here: https://dropbox.github.io/dropbox-api-v2-
          explorer/#files_upload
        // need to consider how this gets secured
        var TOKEN = ''      
        var dir = 'blackground/'
        var file = document.getElementById('file').files[0]     
        var promise = new Promise(function (resolve, reject) {

            // Dropbox requires application/octet-stream
            var xhr = new XMLHttpRequest();

            xhr.onload = function() {
                if (xhr.status === 200) {
                    resolve(JSON.parse(xhr.response));
                }
                else {
                    reject(xhr.response || 'Unable to upload file');
                } 
            };

            xhr.open('POST', 
           'https://content.dropboxapi.com/2/files/upload');
            xhr.setRequestHeader('Authorization', 'Bearer ' + TOKEN);
            xhr.setRequestHeader('Content-Type', 'application/octet-
          stream');
            xhr.setRequestHeader('Dropbox-API-Arg', JSON.stringify({
                path: '/' + dir + file.name,
                mode: 'add',
                autorename: true,
                mute: false
            }));

            xhr.send(file);
        })

        promise
        .then(function (result) {
            // Save dropbox response to form
            document.getElementById('dropbox').value = 
       JSON.stringify(result)

            // Submit form on successful upload
            evt.target.submit()
        })
        .catch(function (err) {
            console.log('a');
        })

        return false
    })

      

It works great. But I want to get each image using Javascript and ajax to render it on my web page. How to do it?

I have read this Documentation https://www.dropbox.com/developers-v1/core/docs#files-GET which we can do with Get Verb.

I have Get for an API to get all images like so:

$.ajax({
  method: "GET",
  url: "https://content.dropboxapi.com/1/files/auto/blackground",
  dataType: "json",
  ContentType:"application/octet-stream",
  Authorization:"Bearer token"
});

      

i am getting this error

    {error: "Authentication failed"}
error
:
"Authentication failed"

      

blackground is the folder that contains all the images

Something might help

+3


source to share


1 answer


It works fine now. This is how I do it. Token is my token for Dropbox.



var token = '';

var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var imageUrl = (window.URL || window.webkitURL).createObjectURL(xhr.response);

        // display, assuming <img id="image"> somewhere
        document.getElementById('image').src = imageUrl;

        // download via the download attribute: limited browser support
        var a = document.createElement('a');
        //a.download = 'test.png';
        //a.href = imageUrl;
        //a.click();
    }
};
xhr.open('POST', 'https://content.dropboxapi.com/2/files/download');
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Dropbox-API-Arg', JSON.stringify({ path: '/blackground/blackground_logo.jpg' }));
xhr.send();

      

0


source







All Articles