Categories

Friday, December 20, 2024
#919814419350 therichposts@gmail.com
Angular 13Bootstrap 5

Angular 13 Upload And Save Image Working Functionality

Angular 13 Upload And Save Image Working Functionality

Hello friends, welcome back to my blog. Today in this blog post, I am going to show, Angular 13 Upload And Save Image Working Functionality.

Working Demo
Angular 13 Upload And Save Image Working Functionality
Angular 13 Upload And Save Image Working Functionality

Angular 13 came and if you are new then you must check below two links:

  1. Angular13 Basic Tutorials
  2. Angular Free Templates

Friends now I proceed onwards and here is the working code snippet and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 13 setup and for this we need to run below commands but if you already have angular 13 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

Guys you can skip this step this step if you already have angular 10 fresh setup:

npm install -g @angular/cli 

ng new angularimageupload //Create new Angular Project

cd angularimageupload  // Go inside the Angular Project Folder

ng serve --open // Run and Open the Angular Project

http://localhost:4200/ // Working Angular Project Url

2. Now friends we need to add below code into your src/app/app.module.ts file:

import { FormsModule }   from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
 
  imports: [
    ...
    FormsModule,
    HttpClientModule
  ],

3. Now friends we need to add below code into src/app/app.component.tsfile:

import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { HttpClient,HttpHeaders } from '@angular/common/http';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent  {
  
  constructor(private http:HttpClient) {}

 
/* file upload */
     /* Variabe to store file data */
     filedata:any;
    /* File onchange event */
    fileEvent(e:any){
        this.filedata = e.target.files[0];
    }
    /* Upload button functioanlity */
    onSubmitform(f: NgForm) {
       
      var myFormData = new FormData();
      const headers = new HttpHeaders();
      headers.append('Content-Type', 'multipart/form-data');
      headers.append('Accept', 'application/json');
      myFormData.append('image', this.filedata);
      /* Image Post Request */
      this.http.post('http://localhost/save.php', myFormData, {
      headers: headers
      }).subscribe(data => {
       //Check success message
       console.log(data);
      });  
  
  }
/* file upload */
}

4. Now friends we need to add below code into src/app/app.component.html file:

<form #f="ngForm" (ngSubmit)="onSubmitform(f)">
  <input type="file" name="myFile" class="form-control mb-3" (change)="fileEvent($event)"/>
    <button class="btn btn-primary">Upload Image</button>
</form>

5. Now friends here is my php code snippet to save angular 13 uploaded file into folder and I added this code into my xampp/htdocs/save.php file:

//Please create uploads folder into xampp/htdocs

<?php

    $target_dir = "uploads/"; //image upload folder name
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    //moving multiple images inside folder
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {

        $data = array("data" => "File is valid, and was successfully uploaded.");
        print json_encode($data);
    }
?>

Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.

Guys in this post, I am uploading image from my angular 13 front-end and save that image inside folder with PHP.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding must watch video above.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.