download files

This commit is contained in:
Peter Maquiran
2024-09-11 12:38:25 +01:00
parent 80e1db12b8
commit 4413ce4698
3 changed files with 39 additions and 39 deletions
+36 -32
View File
@@ -1327,69 +1327,73 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
}
downloadFileFromBrowser(fileName: string, data: any): void {
const linkSource = data;
const downloadLink = document.createElement("a");
downloadLink.href = linkSource;
downloadLink.download = fileName;
downloadLink.click();
downloadFileFromBrowser(fileName: string, msg: MessageViewModal): void {
if(msg.attachments[0]?.blobURl) {
// Create a temporary URL for the Blob
const url = msg.attachments[0].safeFile;
// Create an <a> element with the download attribute
const a = document.createElement('a');
a.href = url;
a.download = fileName; // Set the desired file name
a.click();
} else {
const link = document.createElement("a")
link.href = `data:${msg.attachments[0].mimeType}';base64,${msg.attachments[0].safeFile}`;
link.download = fileName
link.click()
link.remove()
}
}
async openPreview(msg) {
async openPreview(msg: MessageViewModal) {
if (msg.file.type === "application/webtrix") {
this.viewDocument(msg.file, msg.attachments.image_url)
if (msg.attachments[0].source === MessageAttachmentSource.Webtrix) {
this.viewDocument(msg)
} else {
if (!msg.attachments[0].image_url || msg.attachments[0].image_url === null || msg.attachments[0].image_url === '') {
// this.downloadFileMsg(msg)
// this.testDownlod(msg)
if (!msg.attachments[0].safeFile || msg.attachments[0].safeFile === null || msg.attachments[0].safeFile === '') {
} else {
var str = msg.attachments[0].image_url;
str = str.substring(1, ((str.length) - 1));
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
console.log(msg)
if (msg.file.type == "application/img") {
if (msg.attachments[0].mimeType.includes('image')) {
const modal = await this.modalController.create({
component: ViewMediaPage,
cssClass: 'modal modal-desktop',
componentProps: {
image: msg.attachments[0].image_url,
type: msg.file.type,
username: msg.u.name,
_updatedAt: msg._updatedAt
image: msg.attachments[0].safeFile,
type: msg.attachments[0].mimeType,
username: msg.attachments[0].description,
_updatedAt: msg.sentAt
}
});
modal.present();
} else {
this.downloadFileFromBrowser("file", str)
this.downloadFileFromBrowser(msg.attachments[0].title, str)
this.downloadFileFromBrowser(msg.attachments[0].title, str)
this.downloadFileFromBrowser(msg.attachments[0].description, msg)
}
} else {
if (msg.file.type == "application/img") {
if (msg.attachments[0].mimeType.includes('image')) {
const modal = await this.modalController.create({
component: ViewMediaPage,
cssClass: 'modal modal-desktop',
componentProps: {
image: msg.attachments[0].image_url,
type: msg.file.type,
username: msg.u.name,
_updatedAt: msg._updatedAt
image: msg.attachments[0].safeFile,
type: msg.attachments[0].mimeType,
username: msg.attachments[0].description,
_updatedAt: msg.sentAt
}
});
modal.present();
} else {
this.openFile(msg.attachments[0].image_url, msg.attachments[0].title, msg.file.type);
this.openFile(msg.attachments[0].safeFile, msg.attachments[0].description, msg.attachments[0].mimeType);
}
}