cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Surveyor II

Download File; use API?

Jump to solution

Newbie to group

How do I download a file using the API?

The file is a *.docx.
I see GET /api/v1/courses/:course_id/files/:id
That will give me the URL:  https:.../files/id/download?download_frd=1&verifier=XXXX
but it wants to open in my browser, and then it wants me to OPEN in WORD.

I just want to save it (to my desktop or directory).

Thanks

Tags (2)
1 Solution

Accepted Solutions
Highlighted

This worked:  download.file(URL, outfilename, mode='wb')

I tested it with downloading both a *.pdf and *.docx.

Thanks all.

View solution in original post

6 Replies
Highlighted
Navigator

corellie@easternflorida.edu,

What's happening when you use your browser is that it is set to automatically open files of the type application/ms-word or with a file extension of .docx with Word. This is a browser setting, not something you change within Canvas.

You didn't say which browser or operating system that you're using, so I cannot provide specific instructions about how to change this. 

For example, in Chrome on a PC, the file must be saved to disk before you can open it. When you click the button for the downloaded file, what you're really doing is clicking to tell it to open the file. There is an option to ask you where to save the file, but it sounds like you have that turned off so that it always saves it, usually to the \downloads folder. If you navigate there, you may find the file you're looking for.

If you're using Firefox on a PC, then there is a prompt when you go to download that lets you decide what to do with it: open it in an application or save the file. There is also an option to avoid asking each time. Here's an image of the menu that appears (this is not a .docx file, but the premise is the same).

326584_pastedImage_2.png

If you check that box to do this automatically and you chose the "Open with" option, then it will just open in Word without prompting. Here are instructions from Mozilla Support on how to reset that option.

If you're using the API, though, you don't have to download through the browser.

Highlighted
Surveyor II

Thank you for the quick reply.  Not sure I was clear in my question. I use the Canvas LMS REST API to do a variety of tasks (using php and R code). My code(s) execute the required "Get" command

curl 'https://<canvas>/api/v1/courses/<course_id>/files/<file_id>' \       -H 'Authorization: Bearer <token>'

which of course returns a JSON File object (as described in the API documentation). The File object contains the field "url."
In my case, the file is a *.docx.  But it could be a *.html, a *.bmp, or anything. My question is, given the File object URL, how do I
actually download the selected file using the API commands?  I don't want to use the typical "Download" button on the GUI.  I want to
do this programmatically. Thanks for reading.


Highlighted

You don't use the API to download the file. It's a non-API URL. This means that you should not include the Authorization: Bearer header, either. That's what the verifier code is for.

You just need to take that URL and download it using your code and save it. Don't click on the link or paste it into the browser. Make sure you have redirects turned on because there's a good chance that the URL Canvas gives is a nice form to a file somewhere else (probably on Amazon's Web Services).

There may be some language-specific commands you can use to avoid curl, but if you want to use curl, see these examples: 5 Curl Commands to download Files

Highlighted
Surveyor II

Thank you for your help.  I had some success.  In R, I used the command: 

   download.file(URL, outfilename). 

It downloaded, but it has some corruption I am troubleshooting.

Highlighted

corellie@easternflorida.edu,

Try adding mode="wb" to the download.file() function.

mode
character. The mode with which to write the file. Useful values are "w", "wb" (binary), "a" (append) and "ab". Not used for methods "wget" and "curl". See also ‘Details’, notably about using "wb" for Windows.

Highlighted

This worked:  download.file(URL, outfilename, mode='wb')

I tested it with downloading both a *.pdf and *.docx.

Thanks all.

View solution in original post