0

Rust – Upload Files in Actix-web

Last Updated on

This post is about uploading a file or multiple files to a running Actix-web application.

Cargo.tolml

Modify the cargo.toml  as follows.

Server Codes

Use statements

Struct for File Namer

We need to define a struct that implements the FilenameGenerator trait.

Then, we implement the trait.

Note that we are using Uuid to generate unique file names.

Upload Handler

Then, we implement a file upload handler function.

Main function

The main function has the following codes.

When we run the codes, it writes the following output to the console.

Testing

Then, we test our codes using Postman.

Ensure to use form-data.

This is the HTTP response after clicking the Send button.

The files are uploaded to [current-project-dir]/uploaded-images directory.

Tested with Rust 1.37.0.

Avatar

Karl San Gabriel

Professional Software Developer