#7229 download file #833

Merged
jorgep merged 10 commits from 7229-fixDownloadFile into dev 2024-11-22 09:34:32 +00:00
1 changed files with 20 additions and 3 deletions
Showing only changes of commit fb7087d38a - Show all commits

View File

@ -1,11 +1,28 @@
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
import { getUrl } from './getUrl'; import { getUrl } from './getUrl';
import axios from 'axios';
Review

Así en lugar de abrirlo en una ventana de al lado, lo descarga

Así en lugar de abrirlo en una ventana de al lado, lo descarga
import { exportFile } from 'quasar';
const { getTokenMultimedia } = useSession(); const { getTokenMultimedia } = useSession();
const token = getTokenMultimedia(); const token = getTokenMultimedia();
export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) {
let appUrl = await getUrl('', 'lilium'); try {
appUrl = appUrl.replace('/#/', ''); const appUrl = (await getUrl('', 'lilium')).replace('/#/', '');
window.open(url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`); const response = await axios.get(
url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`,
{ responseType: 'blob' }
);
const contentDisposition = response.headers['content-disposition'];
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
const filename =
matches != null && matches[1]
? matches[1].replace(/['"]/g, '')
: 'downloaded-file';
exportFile(filename, response.data);
} catch (error) {
console.error('Error downloading the file', error);
}
} }