Dropbox file upload handler for django

Dropbox announced new pro plans last week and some accounts have had their storage size doubled. Wouldn't it be wonderful if we could upload all our files to dropbox from our django webapp?

In this post, I write a custom file upload handler that will upload files from our application to dropbox.

Let us see how to use the custom file upload handler.

Install the Dropbox Python SDK before you setup your django app to handle the file uploads.

In your settings.py, add the following attributes (with the values filled):


# Optional values below

# The folder where you want the files uploaded.
# Example: /Public or /
# The value below may be either 'app_folder' or 'dropbox'

The DROPBOX_APP_KEY and DROPBOX_APP_SECRET_KEY are provided to you when you create a new dropbox app. Fetching the access token and access token secret is outside the scope of this blog post but you can follow the Getting Started Guide until the Get an access token section and then paste the access token key and secret in the DROPBOX_APP_ACCESS_TOKEN and DROPBOX_APP_ACCESS_TOKEN_SECRET attributes respectively.

Add the DropboxFileUploadHandler to any app (in my case testapp) and reference it in the FILE_UPLOAD_HANDLERS in `settings.py`:


That's it and you are done!

::: {.note} ::: {.admonition-title} Note :::

Since dropbox doesn't support chunked uploads, the file is first uploaded to the temporary file upload directory on the server and then onto dropbox. :::

Here's how you would handle the file post upload in your view:

def file_upload_handler_view(request):
    if request.method == "POST":
        file_uploaded = request.FILES["name_of_file_input"]
        print file_uploaded.read()
    # Helpful attribute to get dropbox file metadata
    # like path on the server, size, thumbnail etc

The DropboxFile returned is an instance of httplib.HTTPResponse and so all file like methods are not defined but some basic methods like read are supported. There is an attribute called dropbox_metadata on the uploaded file that holds the dropbox metadata.

Thank you for reading the Agiliq blog. This article was written by thejaswi on Jul 17, 2012 in upload .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here