How to get audio file name, size, local path, total time duration, file extension using input type file? I am using Angular 2. I am unable to get time duration and local file path.
Once I upload audio file it should go to server with audio file duration. I want to achieve this requirement using only typescript, JavaScript, HTML, Angular 2, without using any third party library.
If any one have idea please let me know. Some code:
html file
<input type="file" name="file" id="file" accept="audio/*" (change)="doBrowse($event)" multiple/>
<button class="btn btn_save" type="button" name="files" (click)="uploadAudio($event)" [disabled]="disableUploadButton">
Upload
</button>
Ts file :
import { Component, OnInit,ViewChild,ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import { ActivatedRoute } from '@angular/router';
import { PostService } from '../../post.service';
import { AppComponent } from '../../app.component';
import { UploadAudioFile } from '../../admin';
const uploadURL = '/upload';
@Component({
selector: 'app-audiofileupload',
templateUrl: './audiofileupload.component.html',
styleUrls: ['./audiofileupload.component.css']
})
export class AudiofileuploadComponent implements OnInit {
list_page=true;
formData:any;
headers:any;
options:any;
file:any;
file_array=[];
fileName_array=[];
fileList:FileList[];
path:any;
fullPath:any;
fullPath_array=[];
slectHospital:any;
slectDepartment:any;
selectDoctor:any;
errorMessage;
localUrl: any[];
fileReader: any;
sourceJson:any;
getPath:any;
fileLength:any;
fileName:any[];
uploadFiles:any;
addUploadFiles:any;
constructor(private postservice: PostService) {
}
ngOnInit() {
this.slectHospital="";
this.slectDepartment="";
this.selectDoctor="";
}
doBrowse(event){
if(!this.slectHospital){
alert("Please Select Hospital ");
return false;
}
if(!this.slectDepartment) {
alert("Please Select Department ");
return false;
}
if(!this.selectDoctor) {
alert("Please Select Doctor");
return false;
}
alert("hospital data :"+this.slectHospital+ ", Dept Data :"+this.slectDepartment+" , hospital data :"+this.slectHospital);
console.log(JSON.stringify("hospital data :"+this.slectHospital+ ", Dept Data :"+this.slectDepartment+" , hospital data :"+this.slectHospital));
this.fileList= event.target.files;
this.fileLength=this.fileList.length;
alert(" length :"+(JSON.stringify(this.fileList.length)));
if(this.fileList && this.fileList.length > 0) {
alert("Inside if block ======== ");
this.file= this.fileList[0];
this.formData=new FormData();
this.path="file://C:/Users/Pawananjay.r/Music/";
for (let i=0; i < this.fileList.length; i++) {
this.file_array[i]=this.fileList[i];
this.fileName_array[i]=this.file_array[i].name;
this.fullPath_array[i]=this.path+this.file_array[i].name;
alert(+i+" in side loop : "+this.fileName_array[i] +","+this.fullPath_array[i]);
}
this.formData.append('uploadFile_key', this.fileName);
alert("file name :"+(JSON.stringify(this.file.name)));
console.log(JSON.stringify("File Name:"+this.file.name));
this.headers=new Headers();
this.headers.append('Content-Type', 'multipart/form-data');
this.headers.append('Accept', 'application/json');
alert("Header Accept:"+this.headers.get("Accept") +"--Header Content Type : "+this.headers.get("Content-Type"));
console.log(JSON.stringify("Header Accept:"+this.headers.get("Accept") +"--Header Content Type : "+this.headers.get("Content-Type")));
this.options = new RequestOptions({ headers: this.headers });
alert("option value :"+(JSON.stringify(this.options)));
console.log(JSON.stringify("option value :"+this.options));
console.log(JSON.stringify("localUrl :"+this.localUrl));
var path1 = (window.URL).createObjectURL(this.file);
console.log('path url :',path1);
alert("path url : "+path1);
}
}
uploadAudio(event){
this.fileList= event.target.files;
alert("Audio File uploaded start");
this.path="file://C:/Users/Pawananjay.r/Music/";
this.fullPath=this.path+this.fileName;
console.log(JSON.stringify("length :"+(JSON.stringify(this.fileLength))));
console.log(JSON.stringify("Full Path :"+this.fullPath));
if(this.fileLength > 0 && this.fileLength!=-1 && this.fileName_array.length>0) {
for (let i=0; i < this.fileLength; i++) {
alert(this.fileReader.readAsArrayBuffer(this.file_array[i]));
alert( this.fileReader.readAsArrayBuffer(this.fileList[0]));
this.file_array[i]=this.fileList[i];
alert(+i+"in side loop : "+this.file_array[i].name);
this.fullPath_array[i]=this.path+this.file_array[i].name;
alert("in side loop 2 :"+this.fullPath_array[i]);
alert("audio size : "+(this.file_array[i].size)/(1024*1024));
alert("file type : "+this.file_array[i].type);
alert("File extension : "+this.file_array[i].name.substring(this.file_array[i].name.lastIndexOf('.'), this.file_array[i].name.length) || this.file_array[i].name);
alert("audio duration : "+this.file_array[i].duration);
}
alert("File Name Array : "+this.fileName_array);
alert("Full Path array :"+this.fullPath_array);
console.log(JSON.stringify("length :"+(JSON.stringify(this.fileLength))));
console.log(JSON.stringify("File Name Array :"+(JSON.stringify(this.fileName_array))));
console.log(JSON.stringify("Full Path array :"+this.fullPath_array));
alert("Audio File uploaded successfully end ");
this.postservice.uploadAudioFiles()
.subscribe(
result=> this.uploadFiles = result,
error => this.errorMessage = <any>error,
);
}else{
alert("Please browse file ! ");
}
}
}