<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Upload Images and save them in a database( Angular 8 + Spring boot + Mysql )]]></title><description><![CDATA[<p dir="auto"><img src="/assets/uploads/files/1563374903888-27c7393e-d31b-4342-8009-59ef72023d9b-image.png" alt="0_1563374901877_27c7393e-d31b-4342-8009-59ef72023d9b-image.png" class=" img-responsive img-markdown" /><br />
This article is all about uploading an image through your angular project , sending it into the backend and saving it in a database.<br />
Not only that, the way how to query the database and viewing back the image in the frontend  is also explained here.<br />
[As this article is written for explaining the simple mechanism of uploading an image,  web page decorations are not done, only the relevant parts are designed for explanation.]</p>
<ul>
<li>
<p dir="auto">The frontend  used is Angular 8.</p>
</li>
<li>
<p dir="auto">The backend use is  Spring boot</p>
</li>
<li>
<p dir="auto">The database used is Mysql</p>
</li>
</ul>
<p dir="auto">Please note that,</p>
<p dir="auto">Some basic knowledge about angular ,spring boot and mysql is essential.( because the way of project creating in those frameworks is not discussed as it makes this article lengthier.)<br />
As IDEs , VS code is used for Angular and IntelliJ IDE  is used for  Spring boot.</p>
<p dir="auto"><strong>1.0 Frontend</strong><br />
First , create an angular project . ( See <a href="https://angular.io/guide/setup-local" target="_blank" rel="noopener noreferrer nofollow ugc">how</a> )<br />
We need HttpClientModule for dealing with the backend. So, We<br />
have to import that module to the project.<br />
Go to <em>src/app/app.module.ts</em></p>
<ul>
<li>
<p dir="auto">Add the following code lines in app.module.ts file as in the image<br />
below.</p>
<pre><code>   import { HttpClientModule } from '@angular/common/http';
</code></pre>
</li>
</ul>
<p dir="auto"><img src="/assets/uploads/files/1563374950982-320b5d8c-f59e-4d12-b1d4-18d2d7bb9b9d-image.png" alt="0_1563374947508_320b5d8c-f59e-4d12-b1d4-18d2d7bb9b9d-image.png" class=" img-responsive img-markdown" /></p>
<pre><code>  and, declare the module in imports array.
</code></pre>
<ul>
<li>Go to app.component.html file and add the following code.</li>
</ul>
<pre><code>&lt;h3&gt;Image Uploading Module&lt;/h3&gt;
&lt;div&gt;
&lt;input type="file" (change)="onFileChanged($event)"&gt;
&lt;img [src]="imgURL" height="200" *ngIf="imgURL"&gt;
&lt;/div&gt;

&lt;!-- upload image  --&gt;

&lt;input type="button" (click)="onUpload()" value="upload"&gt;
&lt;hr /&gt;
Received Image:
{{ receivedImageData['pic'] | json}}

&lt;img [src]="convertedImage" &gt;
</code></pre>
<p dir="auto">The view will be like this.<br />
<img src="/assets/uploads/files/1563375201821-049eedae-2833-41e0-b55a-421b67d2bf75-image.png" alt="0_1563375202159_049eedae-2833-41e0-b55a-421b67d2bf75-image.png" class=" img-responsive img-markdown" /><br />
In this code,<br />
(change)="onFileChanged($event)<br />
is used to listen to any file choosing event and trigger the onFileChange(event) method in .ts file.</p>
<p dir="auto">imgURL, convertedImage are properties in the .ts file.</p>
<p dir="auto">*Go to src/app/app.component.html file and write the following code snippets in it.<br />
</p><section class="float-left"><img src="/assets/uploads/files/1563375281178-63d39039-4011-4fea-98fe-769a81d45a41-image.png" alt="0_1563375278047_63d39039-4011-4fea-98fe-769a81d45a41-image.png" class=" img-responsive img-markdown" /></section><p></p>
<p dir="auto">Import HttpClient class from '@angular/common/http' .<br />
And, inject it via the constructor of the class as in the image.</p>
<p dir="auto"></p><section class="float-left"><img src="/assets/uploads/files/1563375343014-b2712168-0322-4548-90ed-d459b6ec6a9f-image.png" alt="0_1563375341351_b2712168-0322-4548-90ed-d459b6ec6a9f-image.png" class=" img-responsive img-markdown" /></section><br />
Write and complete your app.component.ts file as shown in the images.<p></p>
<p dir="auto">As the backend is running on local port 8080, posting url is also given as preferred.<br />
We send the image to the backend as a FormData,so that  the backend can retrieve it as a multipart file.<br />
By,subscribing to the post method ,we have retrieved the returned image data by the backend and assigned them to the properties which are used to render in the view.</p>
<p dir="auto">***You may find the code snippets here.</p>
<p dir="auto"><a href="https://drive.google.com/file/d/1rZ937j7VOAmM12jauoPg2vcpbFnv3Jid/view" target="_blank" rel="noopener noreferrer nofollow ugc">app.component.ts</a><br />
<a href="https://drive.google.com/file/u/1/d/16nNMKEFLnXIADPxdNvhw7ssvKLOsHxsY/view?usp=drive_open" target="_blank" rel="noopener noreferrer nofollow ugc">app.component.html</a><br />
<a href="https://drive.google.com/file/d/1iK1YBi5o3CVnsDMReg5XlB4CwkOnqxXt/view" target="_blank" rel="noopener noreferrer nofollow ugc">app.module.ts</a></p>
<hr />
<p dir="auto"><strong>2.0 Spring Boot Backend</strong></p>
<p dir="auto">Now,let's move to the backend part.</p>
<p dir="auto"></p><section class="float-left"><img src="/assets/uploads/files/1563375391997-bd2de260-f0c5-4914-8086-d35b77528bdf-image.png" alt="0_1563375391917_bd2de260-f0c5-4914-8086-d35b77528bdf-image.png" class=" img-responsive img-markdown" /></section><p></p>
<p dir="auto">First , create a maven project through <a href="https://start.spring.io/" target="_blank" rel="noopener noreferrer nofollow ugc">Spring Initializer</a> website.</p>
<ol>
<li>Give a group name to the project</li>
<li>Give a artifact nae to the project<br />
3.Add three dependencies given[Spring Web Starter, Spring Data JPA, MySQL Driver, Lombok]</li>
<li>Genereate the project and download the zipped file of the project.</li>
</ol>
<p dir="auto">Extract the zipped file .<br />
Open your IDE(Here,IntelliJ IDE is used).<br />
Open the extracted project by selecting the pom.xml file, through the IDE.</p>
<p dir="auto">This is the file structure in the project I created.</p>
<p dir="auto">A controller class as TestController.java [<a href="https://drive.google.com/open?id=1CNthuTgONE7xC2JtuToofrz5URDFNWTm" target="_blank" rel="noopener noreferrer nofollow ugc">Get code</a>]<br />
A model class as ImageModel.java [<a href="https://drive.google.com/file/d/15mnTDr6zzn_x2-6Lj6YpcWqHVERulmkz/view" target="_blank" rel="noopener noreferrer nofollow ugc">Get Code</a>]<br />
A repository interface as ImageRepository.java [<a href="https://drive.google.com/file/d/1xfDgQUM4OxVXw5YhdAXWcRvFR0IrbGq9/view" target="_blank" rel="noopener noreferrer nofollow ugc">Get code</a>]</p>
<p dir="auto">**ImageModel.java<br />
<img src="/assets/uploads/files/1563375445723-a91f492b-61be-4146-bab0-58f0bf51ba3b-image.png" alt="0_1563375444427_a91f492b-61be-4146-bab0-58f0bf51ba3b-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">**ImageRepository.java<br />
<img src="/assets/uploads/files/1563375455058-09610e67-bedb-4ac4-bee1-9daf22d8dfec-image.png" alt="0_1563375454867_09610e67-bedb-4ac4-bee1-9daf22d8dfec-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">**TestController.java<br />
<img src="/assets/uploads/files/1563375464934-5e491490-c47c-4a19-b873-7c4f34ea88ab-image.png" alt="0_1563375462787_5e491490-c47c-4a19-b873-7c4f34ea88ab-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">Here , in the controller file, image is captured in MulipartFile format . From the frontend, the image data is sent as a form data by appending the data to the request parameter named "myFile" .</p>
<p dir="auto">*You can read about <a href="https://docs.spring.io/spring/docs/3.0.6.RELEASE_to_3.1.0.BUILD-SNAPSHOT/3.1.0.BUILD-SNAPSHOT/org/springframework/web/multipart/MultipartFile.html" target="_blank" rel="noopener noreferrer nofollow ugc">MultipartFile interface here</a>.</p>
<p dir="auto">*You need to edit the application.properties file , to connect to the mysql database according to your PC settings. (Read <a href="https://spring.io/guides/gs/accessing-data-mysql/" target="_blank" rel="noopener noreferrer nofollow ugc">how</a>)</p>
<hr />
<p dir="auto">Enjoy!</p>
]]></description><link>https://lankadevelopers.lk/topic/319/upload-images-and-save-them-in-a-database-angular-8-spring-boot-mysql</link><generator>RSS for Node</generator><lastBuildDate>Wed, 20 May 2026 06:45:56 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/319.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 17 Jul 2019 15:14:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Upload Images and save them in a database( Angular 8 + Spring boot + Mysql ) on Sun, 24 Nov 2019 16:42:06 GMT]]></title><description><![CDATA[<p dir="auto">superb bro</p>
]]></description><link>https://lankadevelopers.lk/post/2518</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2518</guid><dc:creator><![CDATA[root]]></dc:creator><pubDate>Sun, 24 Nov 2019 16:42:06 GMT</pubDate></item><item><title><![CDATA[Reply to Upload Images and save them in a database( Angular 8 + Spring boot + Mysql ) on Wed, 17 Jul 2019 17:02:07 GMT]]></title><description><![CDATA[<p dir="auto">wow amazing</p>
]]></description><link>https://lankadevelopers.lk/post/1926</link><guid isPermaLink="true">https://lankadevelopers.lk/post/1926</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Wed, 17 Jul 2019 17:02:07 GMT</pubDate></item><item><title><![CDATA[Reply to Upload Images and save them in a database( Angular 8 + Spring boot + Mysql ) on Wed, 17 Jul 2019 16:55:35 GMT]]></title><description><![CDATA[<p dir="auto">good post thanks</p>
]]></description><link>https://lankadevelopers.lk/post/1924</link><guid isPermaLink="true">https://lankadevelopers.lk/post/1924</guid><dc:creator><![CDATA[dev_lak]]></dc:creator><pubDate>Wed, 17 Jul 2019 16:55:35 GMT</pubDate></item></channel></rss>