dropboxdropbox-apidropbox-sdk-js

Get link to the uploaded image in dropbox using JS and sdk


I want to get the link of the uploaded file that is uploaded to dropbox using dropbox sdk. I need the links to be stored in another file for a purpose. So far, I have manage to upload the files, but not get the links.

relevant html & JS function I'm using to upload the file:

      <input type="file" id="file-upload" />
function uploadFile() {
        let pathURL;
        
      const UPLOAD_FILE_SIZE_LIMIT = 150 * 1024 * 1024;
      var ACCESS_TOKEN = document.getElementById('access-token').value;
      var dbx = new Dropbox.Dropbox({ accessToken: ACCESS_TOKEN });
      var fileInput = document.getElementById('file-upload');
      var file = fileInput.files[0];
      
      
      if (file.size < UPLOAD_FILE_SIZE_LIMIT) { // File is smaller than 150 Mb - use filesUpload API
        dbx.filesUpload({path: '/' + file.name, contents: file})
          .then(function(response) {
            var results = document.getElementById('results');
            var br = document.createElement("br");
            results.appendChild(document.createTextNode('File uploaded!'));
            results.appendChild(br);
            console.log(response);
            
          })
          .catch(function(error) {
            console.error(error);
          });
      }      return false;
    }

Solution

  • To create a shared link for a file or folder using the official Dropbox API v2 JavaScript SDK, you can use the sharingCreateSharedLinkWithSettings method.

    Here's an example of calling that:

    dbx.sharingCreateSharedLinkWithSettings({path: "/test.txt"})
      .then(function(response) {
        console.log("response:");
        console.log(response);
        console.log("shared link:");
        console.log(response.result.url);
      })
      .catch(function(error) {
        console.error(error);
      });
    

    To get existing shared links, you would use sharingListSharedLinks, which you can call the same way.