multipfilepicker added

This commit is contained in:
Eudes Inácio
2022-03-25 09:25:05 +01:00
parent 8c3fa15aa5
commit edc237124b
8 changed files with 171 additions and 34 deletions
+97 -30
View File
@@ -20,7 +20,7 @@ import { ChatUserStorage } from 'src/app/store/chat/chat-user.service';
import { environment } from 'src/environments/environment';
import { ThemeService } from 'src/app/services/theme.service'
import { Directory, Encoding, FilesystemDirectory } from '@capacitor/filesystem';
import { Directory, Encoding , Filesystem, FilesystemDirectory, } from '@capacitor/filesystem';
import { VoiceRecorder, VoiceRecorderPlugin, RecordingData, GenericResponse, CurrentRecordingStatus } from 'capacitor-voice-recorder';
import { Haptics, ImpactStyle } from '@capacitor/haptics';
import { PreviewCameraPage } from 'src/app/modals/preview-camera/preview-camera.page';
@@ -40,10 +40,9 @@ import { SearchPage } from 'src/app/pages/search/search.page';
import { Storage } from '@ionic/storage';
import { FileToBase64Service } from 'src/app/services/file/file-to-base64.service';
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
import { Plugins } from '@capacitor/core';
import { fromByteArray } from 'base64-js';
import { Plugins, Capacitor } from '@capacitor/core';
import { MultipleDocumentsPicker } from '@awesome-cordova-plugins/multiple-document-picker/ngx';
const { Filesystem } = Plugins;
const IMAGE_DIR = 'stored-images';
@@ -94,6 +93,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
myAudio: any;
downloadfile: any;
downloadFile: any;
files:any[] = [];
constructor(
public popoverController: PopoverController,
@@ -119,6 +119,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
private processesService: ProcessesService,
private storage: Storage,
private fileToBase64Service: FileToBase64Service,
private multipleDocumentsPicker: MultipleDocumentsPicker
) {
this.loggedUser = authService.ValidatedUserChat['data'];
this.roomId = this.navParams.get('roomId');
@@ -673,39 +674,94 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
async addFileToChat(types: typeof FileType[]) {
const roomId = this.roomId
this.multipleDocumentsPicker.pick(2)
.then(async (res: any) => {
this.files = JSON.parse(res)
console.log('1 Add file', this.files[0].uri)
const file: any = await this.fileService.getFileFromDevice(types);
console.log('Add file', file)
/* const imageData = await this.fileToBase64Service.convert(file).then((filee) => {
console.log('Add file', filee)
}) */
const response = await fetch(file);
const blob = await response.blob();
const blob2 = new Blob([blob])
console.log('Add file base64', file)
console.log('Add file blob', blob)
// Here's an example of reading a file with a full file path. Use this to
// read binary data (base64 encoded) from plugins that return File URIs, such as
// the Camera.
const contents = await Filesystem.readFile({
path: this.files[0].uri
});
console.log('data:', contents);
const formData = new FormData();
formData.append("blobFile", blob);
/* const response = await fetch(this.files[0].uri);
const blob = await response.blob(); */
this.wsChatMethodsService.getDmRoom(roomId).send({
file: {
"type": file.type,
"guid": '',
},
attachments: [{
"title": file.name,
"name": file.name,
// "text": "description",
"title_link_download": false,
}],
temporaryData: formData
})
const blob = new Blob([res])
console.log('Add Blob file', blob)
const formData = new FormData();
formData.append("blobFile", blob);
console.log(formData)
this.wsChatMethodsService.getDmRoom(this.roomId).send({
file: {
"type": 'application/pdf',
"guid": '',
},
attachments: [{
"title": 'pdf1',
"name": 'pdf1',
// "text": "description",
"title_link_download": false,
}],
temporaryData: formData
})
})
.catch((error: any) => console.error(error));
/* const roomId = this.roomId
const file: any = await this.fileService.getFileFromDevice(types);
console.log('Add file', file)
const blob = new Blob([file], { type: file.type });
//const blob = new Blob([file.size], {type: file.type });
console.log('Add Blob file', blob)
this.blobToBase64(file)
.then(base64String => console.log(base64String));
const formData = new FormData();
formData.append('blobFile', file);
console.log(formData)
this.wsChatMethodsService.getDmRoom(roomId).send({
file: {
"type": file.type,
"guid": '',
},
attachments: [{
"title": file.name,
"name": file.name,
// "text": "description",
"title_link_download": false,
}],
temporaryData: formData
});
*/
}
blobToBase64 = blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise(resolve => {
reader.onloadend = () => {
resolve(reader.result);
};
});
};
async openChatOptions(ev?: any) {
const roomId = this.roomId
console.log('MOBILE CHAT OPTION', this.members);
@@ -838,6 +894,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
} else {
//console.log('TRY THIS LIBRARY ',fromByteArray(event.body));
this.downloadFile = event.body;
console.log(this._arrayBufferToBase64(event.body))
}
msg.attachments[0] = {
@@ -856,6 +913,16 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
_arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
async openPreview(msg) {
console.log(msg);