fix: Download button opens URL instead of downloading (#10710)

This commit is contained in:
Sivin Varghese
2025-01-17 14:34:44 +05:30
committed by GitHub
parent f2ff3b42e3
commit 3da97d97be
5 changed files with 16 additions and 32 deletions

View File

@@ -5,6 +5,8 @@ import Button from 'next/button/Button.vue';
import Icon from 'next/icon/Icon.vue';
import { useSnakeCase } from 'dashboard/composables/useTransformKeys';
import { useMessageContext } from '../provider.js';
import { downloadFile } from '@chatwoot/utils';
import GalleryView from 'dashboard/components/widgets/conversation/components/GalleryView.vue';
const emit = defineEmits(['error']);
@@ -23,16 +25,8 @@ const handleError = () => {
};
const downloadAttachment = async () => {
const response = await fetch(attachment.value.dataUrl);
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `attachment${attachment.value.extension || ''}`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
const { fileType, dataUrl, extension } = attachment.value;
downloadFile({ url: dataUrl, type: fileType, extension });
};
</script>

View File

@@ -2,6 +2,7 @@
import { computed, onMounted, useTemplateRef, ref } from 'vue';
import Icon from 'next/icon/Icon.vue';
import { timeStampAppendedURL } from 'dashboard/helper/URLHelper';
import { downloadFile } from '@chatwoot/utils';
const { attachment } = defineProps({
attachment: {
@@ -73,17 +74,8 @@ const onEnd = () => {
};
const downloadAudio = async () => {
const response = await fetch(timeStampURL.value);
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = url;
const filename = timeStampURL.value.split('/').pop().split('?')[0] || 'audio';
anchor.download = filename;
document.body.appendChild(anchor);
anchor.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(anchor);
const { fileType, dataUrl, extension } = attachment;
downloadFile({ url: dataUrl, type: fileType, extension });
};
</script>

View File

@@ -3,6 +3,7 @@ import { ref, computed, onMounted } from 'vue';
import { useStoreGetters } from 'dashboard/composables/store';
import { useKeyboardEvents } from 'dashboard/composables/useKeyboardEvents';
import { messageTimestamp } from 'shared/helpers/timeHelper';
import { downloadFile } from '@chatwoot/utils';
import Thumbnail from 'dashboard/components/widgets/Thumbnail.vue';
@@ -117,14 +118,11 @@ const onClickChangeAttachment = (attachment, index) => {
};
const onClickDownload = () => {
const { file_type: type, data_url: url } = activeAttachment.value;
const { file_type: type, data_url: url, extension } = activeAttachment.value;
if (!Object.values(ALLOWED_FILE_TYPES).includes(type)) {
return;
}
const link = document.createElement('a');
link.href = url;
link.download = `attachment.${type}`;
link.click();
downloadFile({ url, type, extension });
};
const onRotate = type => {