cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
If you’ve changed your email address, now's the perfect time to update it on your Dropbox account and we’re here to help! Learn more 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: 

dropbox file download - dbx.filesDownload

dropbox file download - dbx.filesDownload

Tshaniii
Helpful | Level 5
var dbx = new Dropbox({ accessToken: dbx_access_token , fetch});
   
await dbx.filesDownload({
      path: dbx_path
      }).then(async (response) => {
      console.log("down");
      await response.pipe(temporaryFileStream);
    }).catch((error) => {
      console.log(error);
    });
This is the code i am using to download a file. And then I want to open it.
 
So, first i get this 409 error.
status: 409,
headers: HeadersList {
cookies: null,
[Symbol(headers map)]: Map(12) {
'cache-control' => [Object],
'content-security-policy' => [Object],
'x-content-type-options' => [Object],
'content-type' => [Object],
'accept-encoding' => [Object],
'date' => [Object],
'server' => [Object],
'content-length' => [Object],
'strict-transport-security' => [Object],
'x-robots-tag' => [Object],
'x-dropbox-response-origin' => [Object],
'x-dropbox-request-id' => [Object]
},
[Symbol(headers map sorted)]: null
},
error: {
error_summary: 'path/not_found/...',
error: { '.tag': 'path', path: [Object] }
}
}
1. So the path i give to dbx.filesDownload is, path: '/{App-Folder-Name}/{Folder-name}/file.ext
2. Then , i want to know if we download this file, how can I save it to the temprary file path i define (
    const temporaryFilePath = path.join(__dirname, '..', 'temp', `${document.title}`);) .
.. If it is easy I would prefer if you suggest to open this in googledocs with view access only.... and again with write access along with the answer to Q2
 
Thanks!
 
9 Replies 9

Здравко
Legendary | Level 20

Hi @Tshaniii,

The error, you received, means there is no such file as pointed by your dbx_path. Do you able see the pointed file when you list your account using the same token? 🧐 Take in mind that the account views in your web view or local Dropbox folder view can differ from what you see using API if the application uses application folder permissions, not full Dropbox access! So, check that/if you can access particular file (using list) beforethat. If that's the case, you may need to cut your path to contain '/file.ext' only, since '/{App-Folder-Name}/{Folder-name}' becomes your root and if you keep it in the path the Dropbox tries to open '/{App-Folder-Name}/{Folder-name}/{App-Folder-Name}/{Folder-name}/file.ext' - something nonexisting. 😉

About how you can save to file or use particular data; you can do however you want -the same way as can be used any other data - nothing special.

Hope this helps.

 

PS: You're using access token to init your Dropbox client object. Keep in mind that it expires! Move to refresh token usage.

Greg-DB
Dropbox Staff

@Tshaniii Здравко is correct; when using an access token for an app with "app folder" access, paths in your API calls are automatically interpreted relative to the app folder itself, so you shouldn't include the path to the app folder in the path value you supply.

 

And as Здравко said, once you have the data you can use it however you need. You may want to refer to the documentation for your platform's filesystem functionality.

Tshaniii
Helpful | Level 5

Tshaniii_0-1683232300948.png

 

In 

await dbx.filesListFolder({ path: '' }), it prints Regulatory (which is a folder in the application folder - and the permission scope is application)
but , await dbx.filesListFolder({ path: '/Regulatory' }), await dbx.filesListFolder({ path: 'Regulatory' }), await dbx.filesListFolder({ path: 'Regulatory/' }) gives 0 entries with an error like this " error: `Error in call to API function "files/list_folder": request body: path: 'Regulatory/' did not match pattern '(/(.|[\\r\\n])*)?|id:.*|(ns:[0-9]+(/.*)?)'` " .
 
thanks also i need help with saving the downloaded file to a selected location...

 

Здравко
Legendary | Level 20

@Tshaniii, The error, you're receiving just reflect rules that all paths, pointing file/folder in any Dropbox account, should follow to and you have violated. In particular, all paths (except the root) must start with forward slash and no any trailing slashes (both for files and folders). It may be easier to use the paths as appear in your preceding list or use the ids there; they are mandatory correct. This is valid for both downloading and listing nested folders.

Good luck.

Greg-DB
Dropbox Staff

@Tshaniii Здравко is correct, that error is indicating that you didn't provide a valid path format. It's best to take the path value from another API call result, e.g., an entry's 'path_lower' value, but if you write the path value manually it would be of the form '/Regulatory'.

 

When listing a folder like that, it will return an empty list of entries if the folder is empty.

 

The File Access Guide may be a helpful reference for how to interact with the Dropbox filesystem.

Tshaniii
Helpful | Level 5

I added as 

var dbx = new Dropbox({ accessToken: dbx_access_token , fetch});
    console.log(document.Folder.name);
    await dbx.filesListFolder({ path: `/${document.Folder.name}` })
    .then((response) => {
      console.log(response.result.entries);
      console.log('Files and folders in your Dropbox account:', response);
      response.result.entries.forEach((entry) => {

        console.log(entry.name, document.title);

        if ((entry.name).toString() === (document.title).toString()) {
          let file_id = entry.id;
          console.log(file_id);
          dbx.filesDownload({
            path: `${file_id}`
            }).then(async (response) => {
            console.log("down");
            await response.pipe(temporaryFileStream);
          }).catch((error) => {
            console.log(error);
          });
        }
        console.log(entry.name);
      });
    })
    .catch((error) => {
      console.error(error);
    });
 
This code may not be correct as if the folder has subfolders it might not correctly give the entries. So please suggest me how to do this?
 
 
 

app.JPG

 

TypeError: res.buffer is not a function
at D:\GitHubProjects\docMgtSys\document-management-system\node_modules\dropbox\cjs\src\response.js:67:11
at new Promise (<anonymous>)
at parseDownloadResponse (D:\GitHubProjects\docMgtSys\document-management-system\node_modules\dropbox\cjs\src\response.js:61:10)
at D:\GitHubProjects\docMgtSys\document-management-system\node_modules\dropbox\cjs\src\dropbox.js:146:52
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

 

I am urgent in completing this feature. Would you be able to please help me in this.

 

1. App permissions

2. DocFlowController is the app name

3. has 7 folders, History is one of them

4. Inside history there will be subfolders and in them there will be files. sometimes there will be no subfolders but straightaway files.

5. so i want a code to view these files in a editor or somewhere.... i prefer editor becuase there is another feature i implement to open and edit....

 

please give me a code sample for this. would be a great help! 🙂

Greg-DB
Dropbox Staff

@Tshaniii Calling filesListFolder alone is not guaranteed to give you all of the results. You need to use filesListFolderContinue as well. Check out the documentation for more information. You can set FilesListFolderArg.recursive to true when calling filesListFolder if you want to list nested entries too.

 

As for the "TypeError: res.buffer is not a function" error, I'm not sure when/how that's occurring. It doesn't seem to reproduce for me. That's presumably related to the link you currently shared as "await response.pipe(temporaryFileStream);" though. If you're trying to access the file data, that would be in "response.result.fileBinary". If you want to save that to the local filesystem, you should refer to the Node.js filesystem documentation for information on how to do so.

Tshaniii
Helpful | Level 5
    var dbx = new Dropbox({ accessToken: dbx_access_token , fetch});
    console.log(document.Folder.name);
    await dbx.filesListFolder({ path: `/${document.Folder.name}` })
    .then(async (response) => {
      console.log(response.result.entries);
      console.log('Files and folders in your Dropbox account:', response);
      response.result.entries.forEach(async (entry) => {

        console.log(entry.name, document.title);

        if ((entry.name).toString() === (document.title).toString()) {
          let file_id = entry.id;
          console.log(file_id);
          await dbx.filesDownload({
            path: `${file_id}`
            }).then( (response) => {
            console.log("down");
            const data = response.result.fileBinary;
            fs.writeFile(filename, data, (err) => {
              if (err) throw err;
              console.log('File saved!');
            });
          }).catch((error) => {
            console.log(error);
          });
        }
        console.log(entry.name);
      });
    })
    .catch((error) => {
      console.error(error);
    });

    return successMessage(res, 'Document viewed successfully');
  } catch (err) {
    next(err);
  }
};
 
 
so this is all i have in my code and the error i get is ,
TypeError: res.buffer is not a function
at D:\GitHubProjects\docMgtSys\document-management-system\node_modules\dropbox\cjs\src\response.js:67:11
at new Promise (<anonymous>)
at parseDownloadResponse (D:\GitHubProjects\docMgtSys\document-management-system\node_modules\dropbox\cjs\src\response.js:61:10)
at D:\GitHubProjects\docMgtSys\document-management-system\node_modules\dropbox\cjs\src\dropbox.js:146:52
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async D:\GitHubProjects\docMgtSys\document-management-system\controllers\document-controller.js:170:11
 
 
 
 
when i use 
await dbx.filesListFolderContinue({ path: `/${document.Folder.name}` }), 
I get  error: `Error in call to API function "files/list_folder/continue": request body: unknown field 'path'`

Greg-DB
Dropbox Staff

It's still not clear where the "TypeError: res.buffer is not a function" path is coming from; that doesn't reproduce for me when I run this, so it may be something specific to your environment. I recommend referring to your platform's documentation.

 

As for the "Error in call to API function "files/list_folder/continue": request body: unknown field 'path'" error, that's because you're supplying a "path" parameter to the filesListFolderContinue method, however that method doesn't accept a "path" parameter. It only accepts a "cursor" parameter. Please refer to the filesListFolder and filesListFolderContinue documentation for information on how to use these methods.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Tshaniii Helpful | Level 5
What do Dropbox user levels mean?