0

I'm trying to add code on my webserver to download a file from our remote website. (I just used a Microsoft download file as an example)

<p>
<script type = "text/javascript">

document.write('<iframe src="https://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi" size="0" width="0" height="0" style="display: none"></iframe>');

document.write('<table><tr>');
document.write('<td><span style="font-weight: normal; font-size: 20pt; color: #666666;">Thank you for downloading</span></td><br />');
document.write('</tr></table>');

</script>  
</p>

This iframe does not work. I also tried:

<p>
<script type = "text/javascript">

function download(file, text) { 
var element = document.createElement('a'); 
element.setAttribute('href', 'data:text/plain;base64, ' + encodeURIComponent(text)); 
element.setAttribute('download', file); 
document.body.appendChild(element); 
element.click(); 
document.body.removeChild(element); 
} 

document.write('<table><tr>');
document.write('<td><span style="font-weight: normal; font-size: 20pt; color: #666666;">Thank you for downloading</span></td><br />');
document.write('</tr></table>');

var text = "any text"; 
var filename = "https://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi"; 
download(filename, text); 

</script>  
</p>

But this didn't work either. I already tried suggested article/code of Client download of a server generated zip fileAnyone have a sample I can use or what's wrong with the above code?

JeffR
  • 765
  • 2
  • 8
  • 23

1 Answers1

2

Will something like this work for you ?

async function downloadFile(fileData, fileDataContentType) {
try {
    
    const blob = new Blob([fileData], { type: fileDataContentType });
    const link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = 'randomFileName';
    link.click();
} catch (error) {}

}

Zurez
  • 191
  • 2
  • 10
  • What do I put in for fileData and fileDataContentType when the function is called? – JeffR Dec 15 '20 at 18:24
  • File that is whatever you wish the user to download, and the fileContentType is the type of file user will download. For example, to download a png image , the fileData would be the png image data , and fileContentType would be I believe 'image/png' – Zurez Dec 15 '20 at 18:31
  • If it's a zip, exe, or msi, what text goes into fileDataContentType? – JeffR Dec 15 '20 at 18:32
  • For zip you can get the answer from this link https://stackoverflow.com/questions/856013/mime-type-for-zip-file-in-google-chrome#:~:text=According%20to%20w3school%2C%20the%20mime,zip%20file%20is%20application%2Fzip%20. – Zurez Dec 15 '20 at 18:33
  • That code downloads the file, but it downloads 4 copies simultaneously when the "link.click();" is run. However, the code is only run once by putting document.write() in between each line. – JeffR Dec 15 '20 at 18:41
  • Can you add a console.log inside the download function and check how many times it is printed out? – Zurez Dec 15 '20 at 18:42
  • I did the document.write() and it runs just once. – JeffR Dec 15 '20 at 18:44
  • Can you share snippet on how you are using this function? – Zurez Dec 15 '20 at 18:45
  • Sure! Here is the code: ```

    ```
    – JeffR Dec 15 '20 at 18:52
  • 1
    I ran this code here https://jsfiddle.net/s9r0yL4u/ , and file was only downloaded one time. – Zurez Dec 15 '20 at 18:54
  • I did a refresh on the browser and only one came up. Thank you! – JeffR Dec 15 '20 at 19:04
  • If I wanted to show an error if the file didn't exist, how would I code that in the 'catch' with 'error'? – JeffR Dec 15 '20 at 19:05
  • The problem is that it is NOT downloading the full file. Only like 59 bytes. – JeffR Dec 15 '20 at 22:09
  • It is because, you are passing text value (which is the url ) to the function. Before calling this function , you need to call the url , and download data , then pass the data value to this function. – Zurez Dec 16 '20 at 06:58