Recommended method for uploading files for xblocks

For an XBlock that needs to manage files (eg. an audio transcript vtt, a pdf file, some photo images), is there a recommended method?

I’ve seen a few methods:

Thanks. :slight_smile:

Officially, no - the XBlock API doesn’t support uploading files. Unofficially, there are many half-baked ways to do it, as you found.

One thing you didn’t specify is whether the file is from an author or from learners. If it’s from an author (in Studio), then I highly recommend just asking authors to upload the file to “Files & Uploads” (Studio Files Feature)[1], and putting the /static/file_name.type reference in the XBlock’s configuration. This is how the drag-and-drop-v2 XBlock handles files for example, and this is the only option I know of that will preserve the files along with the other course content when the course is exported to a file. Unfortunately there’s no way to do this from the XBlock’s editor UI directly; it’s an awkward two-step process at least.

For learner uploads, I think it’s best to just use Django-storages. You have to manage the permissions and everything yourself (in your XBlock code) because there’s no XBlock API for learner uploads.

In the future, I’d really like to see a proper XBlock API to handle these use cases, but that is blocked by the more urgent need to evolve/replace the XBlock API to solve more urgent problems.


  1. That’s for courses. For content libraries, it works similarly but we support per-XBlock file uploads on the “Details” tab in the Advanced section. ↩︎

2 Likes

Thanks @braden , that makes sense, especially for preserving the files across course export/import. :slight_smile:

I was thinking in terms of author uploads, but it’s helpful to know from the perspective of both.

On the “Files & Uploads” - I found an example of an xblock that uses this directly as a one-step process - the built-in video xblock in fact. For managing the “Handout” file, it works directly with the course Files. (eg. when you upload a handout, it makes a POST request to http://studio.local.openedx.io:8001/assets/course-v1:OpenedX+DemoX+DemoCourse, and you can see the file in http://apps.local.openedx.io:2001/authoring/course/course-v1:OpenedX+DemoX+DemoCourse/assets . Is this a pattern that third party xblocks can use, or could be recommended?

Oh, that’s interesting. I think that should be fine to use; just be aware that it might overwrite files that already exist with the same name.

1 Like