cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
agschmid
Community Contributor

Quizzes: How to add file to file upload question using the API

I'm trying to figure out if it's possible to add a file to a file upload question in quizzes. 

I tested with assignments first (because it was a lot easier!) and the process requires many steps, with the last being that after the file was uploaded to the user's files, sending the file id in the assignment submission.

For quizzes, I've done the file upload and it's in the user's files, then I attempted to actually "take" the quiz through the api. I got as far as /api/v1/courses/:course_id/quizzes/:quiz_id/submissions/questions.

I have found all the other question types and what to add to the json body except for the file upload question:

Quiz Submission Questions - Canvas LMS REST API Documentation 

I was hoping I could just add the ID for the file as I did in assignments, but that doesn't work.

Has anyone been successful at adding a file (as the student) from the user's files to a quiz file upload question?

Tags (3)
0 Kudos
3 Replies
agschmid
Community Contributor

Update: There are actually a couple of issues with the quiz file upload process and I've filed a couple of tickets.

If you test assignments, the folder_id is returned for the user's files -> submissions folder. The assignments upload file is there. 

If you upload a file for quizzes, no folder_id is returned, (displays null), and while you can use the URL returned to view/download the file , it is not found in any of the user's folders. I would have expected it to be in the submissions folder also.

The next problem is that there is no documentation on the file_upload question type, so even if the file were uploaded correctly, it still could not be used if trying to take the quiz through the API.

0 Kudos

Hello @agschmid ,

Did you ever find a solution for this? I need to use the API to upload a file as a question answer, but I can't find anyway to link the file upload to the users quiz submission.

0 Kudos
chrisrockwell
Community Member

After getting through to Level 2 support I finally got an answer on this: post the Id of the file as the answer to the question.

In more detail...

  1. Follow the steps in https://canvas.instructure.com/doc/api/file.file_uploads.html to upload your file to {your-canvas-domain}/api/v1/courses/{courseId}/quizzes/{quizId}/submissions/self/files; You need to use the users bearer token retrieved via OAuth or the masquerade API.
  2. After the final step in either method, you will receive a response that contains an "Id" property
  3. Submit the answer as usual, but use the Id as the answer

I've confirmed this works fine.  It's important to note that the Canvas API does not do any file type validation so that will need to be handled by your application.

0 Kudos