Angular 7 Upload and Dispaly Image Cache
- Updated engagement Jul 01, 2019
- 39.6k
- 12
In this article, nosotros will create a simple Athwart seven application in a step-by-pace manner from scratch to upload multiple images using Spider web API, Athwart seven, and SQL Server.
Introduction
In this commodity, I am going to explain how to upload multiple images using angular 7 and Web API. I am as well creating a demo project for ameliorate understanding. This commodity will assist beginners who are getting started with Angular.
Prerequisites
- Angular 7
- Web API
- SQL Server
- HTML/Bootstrap
Steps required
- Create a database and 2 tables in SQL Server
- Create a Spider web API using ASP.Net Spider web API
- Create a new projection in Angular vii
For this article, I have created a database and two tables. For creating the database, we follow the following steps.
Pace ane
Permit's open SSMS (SQL Server Management Studio).
Step 2
Connect to the SSMS.
Step three
I have created the database using the following query.
- create database db_multipleimage
Step 4
I have created an Imagemaster table.
- CREATE TABLE [dbo].[imagemaster](
- [Id] [int ] IDENTITY(1,i) Non Naught ,
- [ImageName] [varchar ](50) NULL ,
- [Remark] [varchar ](50) NULL ,
- CONSTRAINT [PK_imagemaster] Principal Primal Clustered
- (
- [Id]ASC
- )WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ Main ]
- )ON [ PRIMARY ]
Now, the database looks similar below.
I have created a Web API using ASP.NET Web API. For creating the Web API, we need to follow the following steps.
Stride 1
Open Visual Studio 2017 and get to File > New > Project.
Step 2
Select Spider web >> ASP.NET Web Application.
Step 3
Then, select Web API and click OK.
Step 4
Now, create a new Controller named FileUploadController.
Step 5
Now, import the database using Entity Framework.
Footstep six
Now, create a method (UploadFiles) for inserting images in the table and salvage images in a binder in FileUploadController.
- [HttpPost()]
- public string UploadFiles()
- {
- string sPath = "" ;
- sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/prototype/" );
- var asking = Organisation.Web.HttpContext.Electric current.Request;
- Organization.Web.HttpFileCollection hfc = Organisation.Web.HttpContext.Current.Request.Files;
- var remark = asking["remark" ].ToString();
- for ( int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
- {
- System.Web.HttpPostedFile hpf = hfc[iCnt];
- if (hpf.ContentLength > 0)
- {
- string FileName = (Path.GetFileName(hpf.FileName));
- if (!File.Exists(sPath + FileName))
- {
- hpf.SaveAs(sPath + FileName);
- imagemaster obj =new imagemaster();
- obj.Remark = remark;
- obj.ImageName = FileName;
- wmsEN.imagemasters.Add(obj);
- wmsEN.SaveChanges();
- }
- }
- }
- return "Upload Failed" ;
- }
For this article, I have created an Angular projection using Athwart vii. For creating an Angular project, nosotros need to follow the following steps.
Step 1
I have created a project using the following control in the Command Prompt.
- ng new fileupload
Step 2
Open project in Visual Studio Code using the following commands.
- cd fileupload
- code .
Step iii
Now, we need to install bootstrap in our project using the post-obit command in the last.
- npm install --save bootstrap
Footstep 4
Later on installing bootstrap, we should import bootstrap in style.css.
- @import '~bootstrap/dist/css/bootstrap.min.css' ;
Step v
I have declared methods to upload images using the following lawmaking in App.component.ts
- import { Component } from '@athwart/core' ;
- import { HttpClient } from'@angular/mutual/http' ;
- @Component ({
- selector:'app-root' ,
- templateUrl:'./app.component.html' ,
- styleUrls: ['./app.component.css' ]
- })
- exportclass AppComponent {
- title ='fileupload' ;
- remark ='' ;
- constructor(individual httpService: HttpClient) { }
- myFiles: string[] = [];
- sMsg: string ='' ;
- StudentIdUpdate: string;
- ngOnInit() {}
- getFileDetails(e) {
- //console.log (e.target.files);
- for (var i = 0; i < east.target.files.length; i++) {
- this.myFiles.push(e.target.files[i]);
- }
- }
- uploadFiles() {
- const frmData =new FormData();
- for (var i = 0; i < this.myFiles.length; i++) {
- frmData.append("fileUpload" , this.myFiles[i]);
- if (i == 0) {
- frmData.append("remark" , this.remark);
- }
- }
- this.httpService.post('http://localhost:50401/api/FileUpload/UploadFiles' , frmData).subscribe(
- data => {
- // SHOW A Bulletin RECEIVED FROM THE WEB API.
- this.sMsg = data every bit string;
- console.log(this.sMsg);
- }
- );
- }
- }
Footstep six
Put this lawmaking in app.component.html
- <!--The content below is only a placeholder and tin exist replaced.-->
- <div style="text-align:center" >
- <h1>
- Welcome to {{ title }}!
- </h1>
- <img width="300" alt= "Angular Logo" src= "data:prototype/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==" >
- </div>
- <divclass = "col-sm-five" >
- <ng-container>
- <input blazon="file" id= "file" multiple (alter)= "getFileDetails($event)" >
- <input blazon="text" [(ngModel)]= "remark" >
- <buttonclass = "btn btn-main" (click)= "uploadFiles()" >Upload</push button>
- </ng-container>
- </div>
Now, run the project using the following command.
- ng serve
Summary
In this article, I have discussed how to upload multiple images in a Web API using Angular 7. In my next article, I am going to discuss how to upload a video using Athwart 7 in a Web API.
Source: https://www.c-sharpcorner.com/article/how-to-upload-multiple-image-using-angular7/
0 Response to "Angular 7 Upload and Dispaly Image Cache"
ارسال یک نظر