0

Using dropbox you can create a shortcut by dragging and dropping a URL into your Dropbox folder. This will be saved like this: example of a url in dropbox

Using the /2/files/download HTTP API from dropbox will return an XHR response that looks something like this: raw data returned from dropbox

How do you parse this response so that you can grab only the URL and make that a clickable link?

1

1 Answer 1

0

Here is what needs to go into an Angular 1 factory. To use this, you would just call the downloadFile function from a controller and provide the path to the file in your dropbox account.

 function downloadFile(filePath) {
            if (!filePath) {
                console.error('Cannot download file because no file was specified.');
                return;
            }
            return $q(function(fulfill, reject) {
                $http({
                    url: 'https://content.dropboxapi.com/2/files/download',
                    method: 'POST',
                    headers: {
                        'Authorization': 'Bearer {{access-token-goes-here}}',
                        'Dropbox-API-Arg': `{"path": "${filePath}"}`
                    },
                    responseType: 'blob'
                }).then(
                    results => {
                        // data received from dropbox is binary data saved as a blob
                        // The FileReader object lets web applications asynchronously read the contents of files
                        // https://developer.mozilla.org/en-US/docs/Web/API/FileReader
                        var fileReader = new FileReader();
                         // function will run after successfully reading the file
                        fileReader.onload = function() {
                            var string = this.result; // store the file contents
                            string = encodeURI(string); // get rid of the paragraph return characters
                            var endPosition = string.indexOf('%0D%0A', 32); // find the end of the URL, startPosition is 32
                            var actualURL = string.substring(32, endPosition); // grab only the characters between start and end positions
                            fulfill(actualURL);
                        };
                        fileReader.readAsText(results.data);                           
                    },
                    error => reject(error));
            });
        }
1
  • By the way, it may be a good idea to parse the data fully, as opposed to relying on the desired URL data starting at position 32, since it looks like there are other things that may potentially be included. E.g., see this unofficial guide: lyberty.com/encyc/articles/tech/…
    – Greg
    Commented Dec 21, 2016 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.