Skip to main content
Question

Saving a BOX file Locally

  • May 22, 2025
  • 7 replies
  • 32 views

Forum|alt.badge.img

Hi,

  I have one use case where I have to access downloadurl of a file and save the file on local machine.

The file gets saved but when I try to open it it says file is corrupted.

How do I achieve this?

 

The code snippet is as below.

 

request(obj.downloadUrl .trim(), function(errorresponsebody) {  
        if(obj.downloadUrl != null){                          
            fs.writeFile( c:/test/file_text.pdfnew Buffer(body), function(err) {
                if (errthrow 'error writing file: ' + err;
            });                          
            } 
         }
    });
 
NOTE: 
if I replace obj.downloadUrl .trim() with any other url e.g: http://www.africau.edu/images/default/sample.pdf
correct file gets downloaded and I can even open it without any issues.
 
 
if i paste obj.downloadUrl .trim() directly on the browser correct file gets downloaded without any issues.

7 replies

Forum|alt.badge.img

Hi , I think this is JS, right? Is this the deprecated `requests` library? 

 

My gut feeling would be to verify that the data returned by this request is actually not empty. Is it actually returning data yet not in a readable format, or is it not returning any data at all?

 

 


Forum|alt.badge.img

Hi cbetta,

  It is returning data but not in the expected format. The byte data in the body is not in the expected format. Can I use boxdownloadurl to get the file contents and then save them or is there any other approach to do so? 


Forum|alt.badge.img

  can I just confirm you are using NodeJS? And are you using our SDK or calling the API yourself?


Forum|alt.badge.img

Yes using nodejs

Our company has a box wrapper service written in Java, it uses Java SDK to connect box.

I am simply trying to use downloadurl that this wrapper service returns to download the file using a program.

 

Wrapper service url: http://qa-me......................amazonaws.com/storage/api/repository/file/658550023650
BOX File id :658550023650

downloadUrl is in bold below. (I know it expires so i get a fresh downloadurl every time I try to download.)
Response from the wrapper service.

Response:

{
"createdBy": "feedUser",
"createdDate": 158***phone number removed for privacy***,
"description": "uploaded by batch job",
"downloadUrl": "https://dl.boxcloud.com/d/1/b1!3lgW66ASjWgk8TVTkHNZtBosrggcM52xqFCei40jl-vnTkyi985WMCNxWZpUPl75PnSG52CM58o_QgecTA-Z0MNZOi25crV1LH1nnE-fMn5GXjDjDoV3BNTfZGR56c0pOgMJN_NbQFB365VSUyNIf-gQW9DG6sC2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MfdFChh149jCzxd5apSQL3OiyJzwpeEZ4cftLA_I3HnA9pjFK9uZF6TT6mo13dm7SpEY2GHfnxbzbj_Hi2_w../download",
"id": "658550023650",
"lastModifyingUserName": "feedUser",
"modifiedDate": 1588030621000,
"originalFilename": "1507.pdf",
"currentVersion": 3,
"folderId": "",
"filePath": "All Files/feed_QA/",
"properties": [
{
"key": "modified_by",
"value": "feedUser"
},
{
"key": "creation_date",
"value": "1588030622302"
},
{
"key": "modified_date",
"value": "1588030623262"
},
{
"key": "created_by",
"value": "EfeedUser"
}
],
"isFolder": false,
"folderName": "",
"isPDF": false
}


Forum|alt.badge.img

 Ok, I am not 100% familiar with Java.

 

My gut feeling with this one is that either the encoding of the downloaded file isn't handled properly, or the stream is somehow truncated before it's finished. It's hard to see without seeing all the code that requests the download URL.

 

I'd have a look at our Java SDK and look at the source to see how our SDK handles downloads.

https://github.com/box/box-java-sdk/blob/master/doc/files.md#download-a-file

 


Forum|alt.badge.img

Actually, ignore my reply. You are using Node after all, my bad.


Forum|alt.badge.img

The way I'd normally do this in Node is as follows. This seems to work for a PDF of my own.

let fs = require('fs')
let request = require('request')

let url = 'https://dl....'
let filename = 'file.pdf'

request(url).pipe(fs.createWriteStream(filename)).on('close', () => {
  console.dir('Done')
})

 

I tried your way using the Buffer and that indeed seems to be causing me to get a blank PDF.