简体   繁体   English

从外部URL下载文本文件

[英]Download text file from an external URL

I need to download a text file hosted on a server. 我需要下载服务器上托管的文本文件。

<html>
    <head>
        <title>File</title>
    </head>
    <body>
        <a href="http://example.com/test.txt" download>Click here</a>
    </body>
</html>

With the above code, instead of downloading the file, I am being redirected to the text file. 使用上面的代码,而不是下载文件,而是将我重定向到文本文件。 How do I rectify this? 我该如何纠正?

You can't. 你不能

From https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes 来自https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/a#Attributes

download

This attribute only works for same-origin URLs. 此属性仅适用于同源URL。

t can only download the same file, give you two solutions t只能下载相同的文件,给您两个解决方案

Solution A: You pack your files to. 解决方案A:您将文件打包到其中。 zip/. Rar zip/. Rar and other browsers cannot open the file download. zip/. Rar和其他浏览器无法打开文件下载。

Solution B: forwarded by the back - end, back - end request third-party resources, returned to the front end, front-end using tools such as file-saver save the file. 解决方案B:由后端转发,后端请求第三方资源,返回到前端,前端使用文件保存等工具保存文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM