In this tutorial, we are going to learn how to create Angular 11 application for uploading files to Spring Boot server with progress bar.
2. Architecture
The architecture contains two main layers:
The Angular web application will be responsible for interactions with the end-user. The backend side will handle uploaded files and store them into the filesystem. The relation between the frontend and backend will be RESTful.
3. Spring Boot application
The Spring Boot application provides a single REST service that will handle multipart requests.
3.1. Project structure
The backend Maven project will have the following structure:
The application contains the following classes:
Application - Spring Boot application class that starts the server,
FilesController - Spring controller used for handing HTTP requests,
RestExceptionHandler - class mapping exceptions into HTTP responses,
UploadResponseMessage - Java representation of JSON that provides information about uploading status,
FileService - service that saves uploaded files in the filesystem,
application.properties - Spring Boot application configuration file.
3.2. Maven configuration file
The backend project will be build using Maven.
The pom.xml file has the following structure:
3.2. Rest Controller
The FilesController class is responsible for handling uploaded files.
The uploadFile(...) method is associated with a POST request to files endpoint. It uses the MultipartFile object that handles reference to the uploaded file in a temporary folder. This file will be removed at the end of the request processing.
3.3. Service class
The FileService was introduced to separate business logic from the REST controller. The service is checking if a file with the same uploaded name exists in the folder. If yes the exception is thrown:
3.4. Spring Boot application class
In the Spring Boot application class we configured CORS and upload size limits:
3.5. Application configuration file
The application.properties contains the path for uploaded files:
4. Angular application
To generate the basic Angular project we will use Angular CLI:
The following command will create project angular in the current folder:
And, below commands used to create a service and component:
4.1. Web project structure
The Angular project contains the following files:
In frontend side we could distinguished:
app.module - for import necessary libraries and components,
upload-file.service - using Angular HTTP Clienct to upload files to the backend,
upload-files.component - the main website that contains upload form,
app.component - main application component,
index.html - index HTML website.
4.2. Upload file service
The UploadFileService will use the Angular HTTPClient library to send requests to the Spring Boot server:
Note we set the reportProgress: true, flag to show the progress of every HTTP request call.
4.3. Environment variables
The angular.json file is a place for build instructions and configurations.
Every environment-specific file is represented by the fileReplacements section. If the production configuration is used Angular will replace src/environments/environment.ts with src/environments/environment.prod.ts, so the production settings will be used.
In our case the environment.prod.ts file looks like this:
if we use ng build --configuration=production command to build the project, Angular CLI will perform the file replacement and src/environments/environment.ts is overwritten by src/environments/environment.prod.ts.
Without this special configuration attribute the regular file will be used environments/environment.ts that contains:
This environment file is imported in UploadFileService to get the baseUrl where we will be uploading files: ${environment.baseUrl}/files.
4.4. Upload file component
The UploadFileComponent will be responsible for interactions with user:
The selectFile is called then the user selects the file for upload.
Clicking 'Upload file' will run the upload(): function that sends the file to the backend side.
The upload progress is holded in progress variable:
The HTML component file contains:
input with type="file" for select files for upload,
button - upload file button,
Bootstrap progress bar for presenting upload progress status.
4.5. Application module
In Application module we import HTTP client:
5. Demo
The application presents the following functionality:
5.1. Slow down internet connection for local testing
Normally when testing locally, uploading will be very fast. You won't even notice changing progress bar. Fortunately, there is a way to slow down this process using Chrom Developer Tools.
use the F12 key to open Developer Tools in Chrome browser,
navigate to the Network tab,
change the Online option to Fast 3G or Slow 3G.
done, that's it.
6. Conclusion
In this tutorial, we presented how to build an Angular application for uploading a file with the progress bar.
As usual code used in this article is available in our GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}