Back to All

Upload PDF's name on median

Hi

we have a problem with the pdf's name on android when we want to upload a PDF

That's our code block:

   const downloadFile = window.median?.share?.downloadFile || window.gonative?.share?.downloadFile;

            if (downloadFile) {
                window.median.deviceInfo().then(function (deviceInfo) {
                    window.alert(deviceInfo.platform);
                    if(deviceInfo.platform === 'ios') {
                        downloadFile({url: item.getAttribute('href')});

                        item.dispatchEvent(new Event('epro.stop.loader'));
                    } else if (deviceInfo.platform === 'android') {
                        fetch(item.getAttribute('href'), {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest'
                            },
                            method: 'GET',
                        })
                        .then(response => {
                            if (response.status === 200) {
                                response.blob().then(blob => {
                                    const disposition = response.headers.get('Content-Disposition');
                                    let filename = 'document.pdf';

                                    if (
                                        disposition
                                        && disposition.includes('filename=')
                                    ) {
                                        filename = disposition.replace(/^.*?filename=(.+?)$/, '$1');
                                    }

                                    let file = new File([blob], filename, {
                                        type: blob.type
                                    });
                                    file = window.URL.createObjectURL(file);
                                    window.location.assign(file);
                                });
                            }

                            item.dispatchEvent(new Event('epro.stop.loader'));
                        });
  

window.median?.share?.downloadFile works on IOS devices and we have the right filename. On android this method does nothing on click, if we put an alert just before the call we have the alert but nothing download. With this code, on android with have the download but the filename is always download.pdf even when we force a filename on it.

Thanks