Hi,
i successfully used Media in Ionic 5 to record the save the audio in temp (example : /private/var/mobile/Containers/Data/Application/B620255E-8F53-4163-BF67-DD32E646C0F7/tmp/tmpchat.m4a)
the problem is, i have nodejs api that built on multer and usually takes form-data and upload the files, how to upload that file /private/var/mobile/Containers/Data/Application/B620255E-8F53-4163-BF67-DD32E646C0F7/tmp/tmpchat.m4a
async recordAudio() {
this.file.createFile(this.file.tempDirectory, 'tmpchat.m4a', true).then(() => {
this.mic = 'mic'
let file;
if (this.platform.is('ios')) { //ios
file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + 'tmpchat.m4a');
} else { // android
file = this.media.create(this.file.tempDirectory + 'tmpchat.m4a');
}
file.startRecord();
setTimeout(() => {
file.stopRecord()
this.uploadAudio(file, 1211);
this.mic = 'mic-outline'
}
, 5000);
});
}
async uploadAudio(file, messageId) {
const loader = await this.ui.presentLoader();
const url = 'https://meitstech.net:2223/uploadSupportAttachmentFile/'
const Data = new FormData();
Data.append('SESSION_ID', this.mysession);
Data.append('MESSAGE_ID', messageId);
Data.append('file', file);
let headers = new HttpHeaders();
headers.append('Content-Type', 'multipart/form-data');
var http = new HttpClient(this.handler);
const req = new HttpRequest('POST', url, Data, {
headers: headers
});
http.request(req).subscribe((success: any) => {
this.respondSupport = `<a href=\"${success.body.rows[0].URL}\" target="_blank">tmpchat.m4a</a>`;
loader.dismiss();
this.respondSupport = '';
}, error => {
console.log('myerror', error)
});
}