最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send a link to a webpage and automatically download a file - Stack Overflow

programmeradmin5浏览0评论

so I have a webpage with a element similar to the following

<a href="docs/some_file.xlsx" download="some_file.xlsx" class="btn btn-primary">Download!</a>

Now I'd like to send a hyperlink to some clients via email so that they would access the webpage and at the same time have the file downloaded without them having to click on it. That way they get the file and I make sure that they see the webpage where there is a lot more information.

So far I thought about a right click and copy link to the button directly from the browser but that hyperlink simply downloads the file if I click on it from ab email and I want the browser to pop up and show the landing page where the button and info is. Any idea?

BTW, the webpage is fully static so no php running. Only HTML and javascript could be used to configure the webpage

so I have a webpage with a element similar to the following

<a href="docs/some_file.xlsx" download="some_file.xlsx" class="btn btn-primary">Download!</a>

Now I'd like to send a hyperlink to some clients via email so that they would access the webpage and at the same time have the file downloaded without them having to click on it. That way they get the file and I make sure that they see the webpage where there is a lot more information.

So far I thought about a right click and copy link to the button directly from the browser but that hyperlink simply downloads the file if I click on it from ab email and I want the browser to pop up and show the landing page where the button and info is. Any idea?

BTW, the webpage is fully static so no php running. Only HTML and javascript could be used to configure the webpage

Share Improve this question asked Nov 1, 2021 at 18:12 guigomchaguigomcha 781 gold badge2 silver badges6 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

What you could do - setup a certain parameter for example: https://example./?autodownload=1

then add a piece of javascript that either imediately, or after some timeout automatically downloads the file. Something like:

document.addEventListener("DOMContentLoaded", function(event) { 
    if (window.location.href.indexOf("autodownload") > -1)
      setTimeout(function(){ 
            window.open("https://example./docs/some_file.xlsx");
        }, 3000); //your timeout in miliseconds
    }
});

of course this solution will not work, if browser will block opening new windows. Second possibility with this approach is to replace the window.open function with click on the anchor itself.

You can trigger a file download with JavaScript by firing click() on an <a> tag with the download attribute. Something like this:

const $link = document.createElement('a');
$link.href = 'docs/some_file.xlsx';
$link.download = 'some_file.xlsx';
$link.click();

Keep in mind, however, that browsers may try to limit automatic file downloads in order to prevent sites doing dodgy things. I've seen this in particular when a site tries to download multiple files, so I think you'll be fine just downloading a single file.

Generally, the best way around this is to ensure that you only perform this action in response to user input, such as a click, but that doesn't sound like it would work for the solution you're looking for here.

How To Add Automatically Download Link in HTML

It is very easy to make a automatically download link in HTML

And i am going to showe you all how to make a automatically link in HTML (In 5 steps)

  1. Open your Code editor app where are you do codes and create one HTML document

  2. Once you create an HTML document in your Code editor app, Now add  Tag and Href Attribute in your HTML Documents

  3. After adding  Tag and Href Attribute in your HTML Documents, Now Provide the File or Folder location in your Href Attribute

  4. After adding the File or Folder location in your Href Attribute, Now Add the Download Attribute in your Tag

  5. After Adding the Download Attribute in your Tag, Now Save the HTML File and Check the download Link

For Example

<a href="images.png/cat.png" download>Download</a>

you can also check out this video

https://www.youtube./watch?v=1eQQCEMeRfE

I tried the <a download="filename.type"> method and also forcing the headers but Gmail I think removes the download attribute from the link so it doesnt automatically download.


Instead of directly providing the download URL, we can create a new endpoint in our server that will act as a proxy for the file download. This new endpoint will fetch the file from Storage and then serve it with the appropriate headers to force a download.

发布评论

评论列表(0)

  1. 暂无评论