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

  1. Create a database and 2 tables in SQL Server
  2. Create a Spider web API using ASP.Net Spider web API
  3. 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).

How To Upload Multiple Image Using Angular 7

Step 2

Connect to the SSMS.

How To Upload Multiple Image Using Angular 7

Step three

I have created the database using the following query.

  1. create database  db_multipleimage

Step 4

I have created an Imagemaster table.

  1. CREATE TABLE  [dbo].[imagemaster](
  2.     [Id] [int ] IDENTITY(1,i) Non Naught ,
  3.     [ImageName] [varchar ](50) NULL ,
  4.     [Remark] [varchar ](50) NULL ,
  5. CONSTRAINT  [PK_imagemaster] Principal Primal  Clustered
  6. (
  7.     [Id]ASC
  8. )WITH  (PAD_INDEX  = OFF , STATISTICS_NORECOMPUTE  = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS  = ON , ALLOW_PAGE_LOCKS  = ON ) ON  [ Main ]
  9. )ON  [ PRIMARY ]

Now, the database looks similar below.

How To Upload Multiple Image Using Angular 7

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.

How To Upload Multiple Image Using Angular 7

How To Upload Multiple Image Using Angular 7

Step 2

Select Spider web >> ASP.NET Web Application.

How To Upload Multiple Image Using Angular 7

Step 3

Then, select Web API and click OK.

How To Upload Multiple Image Using Angular 7

Step 4

Now, create a new Controller named FileUploadController.

How To Upload Multiple Image Using Angular 7

How To Upload Multiple Image Using Angular 7

Step 5

Now, import the database using Entity Framework.

How To Upload Multiple Image Using Angular 7

Footstep six

Now, create a method (UploadFiles) for inserting images in the table and salvage images in a binder in FileUploadController.

  1. [HttpPost()]
  2. public string  UploadFiles()
  3.        {
  4. string  sPath = "" ;
  5.            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/prototype/" );
  6.            var asking = Organisation.Web.HttpContext.Electric current.Request;
  7.            Organization.Web.HttpFileCollection hfc = Organisation.Web.HttpContext.Current.Request.Files;
  8.            var remark = asking["remark" ].ToString();
  9. for  ( int  iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
  10.            {
  11.                System.Web.HttpPostedFile hpf = hfc[iCnt];
  12. if  (hpf.ContentLength > 0)
  13.                {
  14. string  FileName = (Path.GetFileName(hpf.FileName));
  15. if  (!File.Exists(sPath + FileName))
  16.                    {
  17.                        hpf.SaveAs(sPath + FileName);
  18.                        imagemaster obj =new  imagemaster();
  19.                        obj.Remark = remark;
  20.                        obj.ImageName = FileName;
  21.                        wmsEN.imagemasters.Add(obj);
  22.                        wmsEN.SaveChanges();
  23.                    }
  24.                }
  25.            }
  26. return "Upload Failed" ;
  27.        }

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.

  1. ng new  fileupload

Step 2

Open project in Visual Studio Code using the following commands.

  1. cd fileupload
  1. code .

How To Upload Multiple Image Using Angular 7

Step iii

Now, we need to install bootstrap in our project using the post-obit command in the last.

  1. npm install --save bootstrap

Footstep 4

Later on installing bootstrap, we should import bootstrap in style.css.

  1. @import '~bootstrap/dist/css/bootstrap.min.css' ;

Step v

I have declared methods to upload images using the following lawmaking in App.component.ts

  1. import { Component } from '@athwart/core' ;
  2. import { HttpClient } from'@angular/mutual/http' ;
  3. @Component ({
  4.   selector:'app-root' ,
  5.   templateUrl:'./app.component.html' ,
  6.   styleUrls: ['./app.component.css' ]
  7. })
  8. exportclass  AppComponent {
  9.   title ='fileupload' ;
  10.   remark ='' ;
  11.   constructor(individual httpService: HttpClient) { }
  12.   myFiles: string[] = [];
  13.   sMsg: string ='' ;
  14.   StudentIdUpdate: string;
  15.   ngOnInit() {}
  16.   getFileDetails(e) {
  17.     //console.log (e.target.files);
  18. for  (var i = 0; i < east.target.files.length; i++) {
  19.       this.myFiles.push(e.target.files[i]);
  20.     }
  21.   }
  22.   uploadFiles() {
  23.     const frmData =new  FormData();
  24. for  (var i = 0; i < this.myFiles.length; i++) {
  25.       frmData.append("fileUpload" , this.myFiles[i]);
  26. if  (i == 0) {
  27.         frmData.append("remark" , this.remark);
  28.       }
  29.     }
  30.     this.httpService.post('http://localhost:50401/api/FileUpload/UploadFiles' , frmData).subscribe(
  31.       data => {
  32.         // SHOW A Bulletin RECEIVED FROM THE WEB API.
  33.         this.sMsg = data every bit string;
  34.         console.log(this.sMsg);
  35.       }
  36.     );
  37.   }
  38. }

Footstep six

Put this lawmaking in app.component.html

  1. <!--The content below is only a placeholder and  tin exist replaced.-->
  2. <div style="text-align:center" >
  3.   <h1>
  4.     Welcome to {{ title }}!
  5.   </h1>
  6.   <img width="300"  alt= "Angular Logo"  src= "data:prototype/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==" >
  7. </div>
  8. <divclass = "col-sm-five" >
  9.   <ng-container>
  10.       <input blazon="file"  id= "file"  multiple (alter)= "getFileDetails($event)" >
  11.       <input blazon="text"  [(ngModel)]= "remark" >
  12.       <buttonclass = "btn btn-main"  (click)= "uploadFiles()" >Upload</push button>
  13.   </ng-container>
  14. </div>

Now, run the project using the following command.

  1. ng serve

How To Upload Multiple Image Using Angular 7

How To Upload Multiple Image Using Angular 7

How To Upload Multiple Image Using Angular 7

How To Upload Multiple Image Using Angular 7

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.

carterwharyince.blogspot.com

Source: https://www.c-sharpcorner.com/article/how-to-upload-multiple-image-using-angular7/

0 Response to "Angular 7 Upload and Dispaly Image Cache"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel