Home Angular 10 Angular 10 Laravel 8 Image Upload Tutorial

Angular 10 Laravel 8 Image Upload Tutorial

by therichpost
Published: Updated: 6 comments
Angular 10 Laravel 8 Image Upload Tutorial

Hello friends, welcome back to my blog. Today in this blog post, I am going to show, Angular 10 Laravel 8 Image Upload Tutorial.

Guys we can use same code snippet for Angular 12 Laravel 8 Image Upload. I checked it and it will work fine.

Angular 10 Image Upload

Angular12 came and if you are new then you must check below link:

  1. Angular12 Basic Tutorials

Friends now I proceed onwards and here is the working code snippet for Angular 10 Laravel 8 Image Upload Tutorial and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 10 setup and for this we need to run below commands but if you already have angular 10 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, here we need to run below commands into our project terminal to install sweetalert2  modules into our angular application:

npm install --save sweetalert2

ng serve --o

3. Now friends, here we need to add below  into our angular.json file to get modules styles and scripts:

...
"styles": [
              ...
              
               "node_modules/sweetalert2/src/sweetalert2.scss",
              
            ],
            "scripts": [
              ...
             
              "node_modules/sweetalert2/dist/sweetalert2.js", 
              
            ]
...

 

4. 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
  ],

 

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

import {NgForm} from '@angular/forms';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import Swal from 'sweetalert2/dist/sweetalert2.js';

export class AppComponent {

...
 constructor(private http:HttpClient) { }
/* file upload */

     /* Variabe to store file data */
     filedata:any;

    /* File onchange event */
    fileEvent(e){
        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/laravel8/public/api/sample-restful-apis', myFormData, {
      headers: headers
      }).subscribe(data => {

       //Check success message
       //sweetalert message popup
        Swal.fire({
             title: 'Hurray!!',
             text:   data['message'],
             icon: 'success'
         });
      });  
  
  }
/* file upload */
}

6. 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" (change)="fileEvent($event)"/>
    <button>Upload Image</button>
</form>

7. Now friends, we need to add below code into our laravel 8 project routes/api.php file:

<?php
...
use App\Http\Controllers\HomeController;
...
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/


...
Route::post('/sample-restful-apis', [App\Http\Controllers\HomeController::class, 'uploadimage'])->name('sample-restful-apis');
...

 

8. Now friends, we need to add below code into our laravel 8 project app/Http/Controllers/HomeController.php file:

<?php

...

class HomeController extends Controller
{
...

//File Upload Function
public function uploadimage(Request $request)
    {
      //check file
      if ($request->hasFile('image'))
      {
            $file      = $request->file('image');
            $filename  = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $picture   = date('His').'-'.$filename;

            //move image to public/img folder
            $file->move(public_path('img'), $picture);
            return response()->json(["message" => "Image Uploaded Succesfully"]);
      } 
      else
      {
            return response()->json(["message" => "Select image first."]);
      }
    }

}

 

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 10 front-end and save that image inside folder with Laravel 8.

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

You may also like

6 comments

Chetana Chaudhari September 19, 2020 - 1:55 pm

Actually i am new in angular and I want upload video code in angular and MySQL.
1. video upload with Angular+PHP+MySQL
2. Upload video store in upload folder and information (id, name, location, size, type)store in temp table.
3. next page is display on on-click event(upload button), and some information like metadata, meta tile, image_url, trailer_name.
4. After filling information and click on submit button the all information store in MySQL database as well as remove temp table data and store in actual upload_video_table.

can you help me to complete this task.

Reply
Ajay Malhotra September 19, 2020 - 3:39 pm

What type of video? YouTube video link? embedded code?

Reply
Chetana Chaudhari September 20, 2020 - 1:17 pm

youtube downloaded video.

Reply
Ajay Malhotra September 20, 2020 - 4:30 pm

Okay.

Reply
Chetana September 21, 2020 - 2:50 pm

Thanks

Reply
Chetana September 21, 2020 - 2:51 pm

can you send me on my Mail ID

Reply

Leave a Comment

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