乐闻世界logo
搜索文章和话题

How can I create download link in HTML?

1个答案

1

To create download links in HTML, use the <a> tag and set the href attribute to the path of the target file. Additionally, to ensure the browser interprets the link as a downloadable file rather than a new page to open, employ the download attribute. This attribute instructs the browser to download the linked resource instead of navigating to it.

Here is a simple example:

html
<!DOCTYPE html> <html> <head> <title>Download Page</title> </head> <body> <h1>Download Example</h1> <a href="path/to/your/file.pdf" download="NewFileName.pdf">Download PDF File</a> </body> </html>

In this example, the href attribute of the <a> tag is set to the path of the file you want to download, which is path/to/your/file.pdf. The download attribute is set to NewFileName.pdf, meaning that when the user clicks the link to download the file, it will be saved as NewFileName.pdf instead of the original filename.

The advantage of this method is its simplicity and compatibility with most modern browsers. However, note that if the file is cross-origin or the browser does not support the download attribute (such as in older versions), the attribute may not function. In such cases, the browser will attempt to open the file instead of downloading it. Therefore, it is advisable to ensure that website visitors use modern browsers that support HTML5.

2024年6月29日 12:07 回复

你的答案