Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angualar image upload service

postFile(fileToUpload: File): Observable<boolean> {
    const endpoint = 'your-destination-url';
    const formData: FormData = new FormData();
    formData.append('fileKey', fileToUpload, fileToUpload.name);
    return this.httpClient
      .post(endpoint, formData, { headers: yourHeadersConfig })
      .map(() => { return true; })
      .catch((e) => this.handleError(e));
}
Comment

image file upload in angular

import { Component, OnInit } from '@angular/core';
import { FileUploadService } from './file-upload.service';
  
@Component({
    selector: 'app-file-upload',
    templateUrl: './file-upload.component.html',
    styleUrls: ['./file-upload.component.css']
})
export class FileUploadComponent implements OnInit {
  
    // Variable to store shortLink from api response
    shortLink: string = "";
    loading: boolean = false; // Flag variable
    file: File = null; // Variable to store file
  
    // Inject service 
    constructor(private fileUploadService: FileUploadService) { }
  
    ngOnInit(): void {
    }
  
    // On file Select
    onChange(event) {
        this.file = event.target.files[0];
    }
  
    // OnClick of button Upload
    onUpload() {
        this.loading = !this.loading;
        console.log(this.file);
        this.fileUploadService.upload(this.file).subscribe(
            (event: any) => {
                if (typeof (event) === 'object') {
  
                    // Short link via api response
                    this.shortLink = event.link;
  
                    this.loading = false; // Flag variable 
                }
            }
        );
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: flutter firebase notification sound 
Javascript :: react time input 
Javascript :: react native font awesome 
Javascript :: javascript import 
Javascript :: ternary operator in button react 
Javascript :: javascript next friday 
Javascript :: ngif is string angular 
Javascript :: C# Convert DataTable to Json File using Newtonsoft.Json DLL 
Javascript :: find max value in array javascript 
Javascript :: hasownproperty javascript 
Javascript :: Date object for local time and date 
Javascript :: move element onclick javascript 
Javascript :: how to target an element inside of a $(this) jquery 
Javascript :: tolocale string no seconds 
Javascript :: react form hook npm 
Javascript :: js socket.emit 
Javascript :: Find the index of an item in the Array 
Javascript :: javascript check date is greater than today 
Javascript :: trigger keyboard event javascript 
Javascript :: SyntaxError: await is only valid in async function 
Javascript :: select parent of element jquery 
Javascript :: electron load index.html 
Javascript :: Javascript - find the largest 
Javascript :: check browser 
Javascript :: show a div in jquery 
Javascript :: line separator with text in the center react native 
Javascript :: SHOPIFY LANGUAGE SELECTOR 
Javascript :: javascript array clear 
Javascript :: expo modal 
Javascript :: javascript + sync for loop 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =