0

I need to automate download of a pdf file. Sample link below

<a href="https:\\..?serachid=1" 
onclick="window.open(this.href,'popupwindow','scrollbars=no,resizable=no')return false" >click to 
download</a>

Link's href doesn't have .pdf at the end. on clicking on the link, I am getting IE popup which has embedded pdf. clicking on print from popup makes a processing window which takes quit a lot of time and we are unable to track the progress. Rather clicking on save a copy button ( from flier which appears below works fine) if I click it manually, but I couldn't find a way to click on same from code.)

I am looking for option to right click and clicking on "save target as" or option to click the "save a copy" buttonenter image description here I have tried quit a lot of suggestions still I am unable to find a solution which works for me, please help.

Note: I can't modify html. and I can't use selenium.

[Edit] I have tried to add download attribute as follows, still am getting IE popup, not direct download option as expected.

<a href="https:\\..?serachid=1" 
onclick="window.open(this.href,'popupwindow','scrollbars=no,resizable=no')return false" download="">click to 
download</a>
  • Does this answer your question? [(HTML) Download a PDF file instead of opening them in browser when clicked](https://stackoverflow.com/questions/6794255/html-download-a-pdf-file-instead-of-opening-them-in-browser-when-clicked) – Heretic Monkey Nov 11 '20 at 15:25
  • Thanks for your response, I tried adding download attribute but doesn't work for me – Karthiga Sampath Nov 11 '20 at 15:36
  • Please [edit] your question to show how you implemented the download attribute and what "doesn't work for me" means. – Heretic Monkey Nov 11 '20 at 15:43
  • Even after adding download attribute, I am getting the same IE popup – Karthiga Sampath Nov 11 '20 at 16:16
  • Yeah, that's not how you use the download attribute. You remove the `onclick` attribute (which opens a popup) and set the download attribute to the default name of the file, e.g. `download="searchId1.pdf"` It's still going to ask the user where to save the file and the name of the file; that's for security (imagine if websites could save arbitrary files wherever they wanted to -- "I'm going to save malware at C:\Program Files\Google\Chrome\chrome.exe". – Heretic Monkey Nov 11 '20 at 17:02
  • @HereticMonkey Thanks for clarifying this, let me try and get back:) – Karthiga Sampath Nov 11 '20 at 17:16
  • Hey I have removed onclick attribute and added download attribute, now I am getting the current screen replaced instead of IE popup, but I didn't get the download option – Karthiga Sampath Nov 11 '20 at 17:31
  • You may need to try one of the other answers on the duplicate. [This one for instance, does not depend on the download attribute](https://stackoverflow.com/a/34729861/215552). – Heretic Monkey Nov 11 '20 at 17:40
  • @YuZhou This solution works great in Chrome, however I am getting "object doesn't support property or method 'msSaveBlob'" in IE, My version is 11.0.9600.X – Karthiga Sampath Nov 18 '20 at 08:15
  • It's so strange. How about directly input `window.navigator.msSaveBlob` in console in IE? This should be undefined in Chrome and work in IE 11. My IE 11 version is 11.630.19041.0 and you can see from my screenshot, it works fine in my IE. – Yu Zhou Nov 18 '20 at 08:35
  • I tried in both IE and chrome with `window.navigator.msSaveBlob` both throws error. I am using this in client environment, is there is possible miss of IE patch or something there? – Karthiga Sampath Nov 18 '20 at 12:29
  • Do I need to enable something? to resolve this in IE – Karthiga Sampath Nov 19 '20 at 05:10
  • The code is right, I think the issue might be related with IE settings. I found a [similar thread](https://stackoverflow.com/questions/35194359/navigator-mssaveblob-function-is-not-working-in-ie11), you can try the solution in it. If it still not work, please try to [reset IE settings](https://support.microsoft.com/en-us/topic/change-or-reset-internet-explorer-settings-2d4bac50-5762-91c5-a057-a922533f77d5) or [reinstall IE](https://support.microsoft.com/en-us/help/318378/how-to-repair-or-reinstall-internet-explorer-in-windows). – Yu Zhou Nov 19 '20 at 08:08
  • @YuZhou I couldn't reset IE properties since it's citrix environment and is controlled by group policy, is there a way to make it work by watin code?, the thread you have attached above has different problem for me IE doesn't recognize the msSaveBlob method – Karthiga Sampath Nov 20 '20 at 13:27
  • I have no other idea about how to solve this except repairing IE. There must be something wrong with IE if it can't recognize `msSaveBlob`. – Yu Zhou Nov 23 '20 at 02:08

1 Answers1

1

IE doesn't support download attribute in <a> so it doesn't work in IE. You can check the browser compatibility table.

For IE, if you want to click the link to download, you can only convert the pdf file to blob data then download it using msSaveBlob. msSaveBlob is IE's own API for creating and downloading files. You can refer to the code below:

<a href="your_file_link" onclick="downloadpdf();return false">click to download</a>
<script>
    function downloadpdf() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                //console.log(this.response, typeof this.response);
                window.navigator.msSaveBlob(this.response, 'testpdf.pdf'); 
            }
        }
        xhr.open('GET', 'your_file_link');
        xhr.responseType = 'blob';
        xhr.send();  
    }
</script>

Result in IE 11: enter image description here

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • This solution doesn't works for me from citrix, however from my local machine I am able to achieve it, some configuration is missing in citrix, all in all solution is working. – Karthiga Sampath Nov 24 '20 at 05:29