Saving a BOX file Locally

Highlighted
First-time Contributor

Saving a BOX file Locally

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 7
Highlighted
Box Employee

Re: Saving a BOX file Locally

Hi @kanan_692020, 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?

 

 


---
Developer Advocate @ Box
Highlighted
Occasional Contributor

Re: Saving a BOX file Locally

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? 

Highlighted
Box Employee

Re: Saving a BOX file Locally

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


---
Developer Advocate @ Box
Highlighted
Occasional Contributor

Re: Saving a BOX file Locally

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-vnTkyi985WMCNxWZpUPl75PnSG5...",
"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
}

Highlighted
Box Employee

Re: Saving a BOX file Locally

@kanang29 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

 


---
Developer Advocate @ Box
Box Employee

Re: Saving a BOX file Locally

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


---
Developer Advocate @ Box
Highlighted
Box Employee

Re: Saving a BOX file Locally

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.

 

 

 

 


---
Developer Advocate @ Box