Angular 5 must click to continue after HTTP Post request

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Angular 5 must click to continue after HTTP Post request



I am working on an Angular 5 component that includes "ngx-file-drop" and "ngx-spinner" modules.
The issue appears after the upload of a file via my API.
The file is correctly uploaded, and on my subscribe function, it shows correctly the console.log line but not the others, so I have to click on the screen to continue.



in the code below, I tried to add an eventEmitter to handle the and of the upload. It is correctly triggered but the "this.spinner.hide() and this.entryFile = null" are not executed until I click on the screen.


import { Component, OnInit, EventEmitter } from '@angular/core';
import { FileSystemDirectoryEntry, UploadFile, FileSystemFileEntry, UploadEvent } from 'ngx-file-drop';
import { TranslateService } from '@ngx-translate/core';
import {NgxSpinnerService} from 'ngx-spinner';
import { HttpClient } from '@angular/common/http';
import { ConfirmSavedModalContent } from '../../../shared/form/confirm-saved-modal.content';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

@Component({
templateUrl: './import.component.html'
})
export class PoiImportComponent implements OnInit {
resourceUrl = 'api/files/upload';
public fileEntry: UploadFile;
public wrongFileType:boolean;
public finishReceiver = new EventEmitter();
constructor(
private http: HttpClient,
private modalService: NgbModal,
public spinner : NgxSpinnerService,
private translate: TranslateService) {
var lang = window.sessionStorage.getItem("LANG");
if (lang !== undefined && lang !== null){
translate.setDefaultLang(lang);
}
else{
translate.setDefaultLang('en');
}
}

ngOnInit() {
//receives the end of the file upload.
this.finishReceiver.subscribe(resolve=>{
this.spinner.hide();
this.fileEntry = null;
});

}



public dropped(event: UploadEvent) {

//Get file dropped
var tmp = event.files[0];

//Split name to get extention
var name = tmp.relativePath.split(".");

//Check if the extension is right
this.wrongFileType = (name[1] !== "csv");
if (!this.wrongFileType){
this.fileEntry = tmp;
}
}

//Show alert of wrong file extension
public fileLeave(event){
this.wrongFileType = false;
}

clearFile(){
this.fileEntry = null;
this.hideAlert();
}

hideAlert(){
this.wrongFileType = false;
}

uploadFile(){

//check if the entry is a valid file
if (this.fileEntry.fileEntry.isFile){

//Cast entry to file System
const upload = this.fileEntry.fileEntry as FileSystemFileEntry;

//Show loading spinner
this.spinner.show();

//Get the file from dropped data
upload.file((file:File)=>{

//Create formData from the file dropped
let input = new FormData();
input.append('name',file.name);
input.append('file', file);
this.http.post<File>(this.resourceUrl, input).subscribe(resolve =>{
//Send finish upload emit
this.finishReceiver.emit(resolve);
});
});
}else{
this.spinner.hide();
}
}

}




1 Answer
1



Try below code and check again :


uploadFile(){
$mainRef = this.
//check if the entry is a valid file
if (this.fileEntry.fileEntry.isFile){

//Cast entry to file System
const upload = this.fileEntry.fileEntry as FileSystemFileEntry;

//Show loading spinner
this.spinner.show();

//Get the file from dropped data
upload.file((file:File)=>{
//Create formData from the file dropped
let input = new FormData();
input.append('name',file.name);
input.append('file', file);
this.http.post<File>(this.resourceUrl, input).subscribe(resolve =>{
//Send finish upload emit
$mainRef.finishReceiver.emit(resolve);
});
});
}else{
this.spinner.hide();
}



}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

C++ virtual function: Base class function is called instead of derived