简体   繁体   English

如何创建一个按钮或链接来自动下载基于 URL 的文件?

[英]How can I create a button or link that automatically downloads a file based on a URL?

Here's a website: Download link generator这是一个网站:下载链接生成器

And here's the form in the page source:这是页面源代码中的form

<form id="download" class="download_action" method="POST">
<input type="hidden" name="action" value="download_action">
<label for="url">Please enter the URL you would like to save as a file:</label>
<div class="field">
<input id="url" class="text mr-3" name="keyword" type="text">
<input class="submit" title="Save target as" type="submit" value="Save target as">
</div>
<div class="tool-target" style="padding-bottom: 10px;"> </div>
<div id="return"> </div>
<input type="hidden" id="_wpnonce" name="_wpnonce" value="5c9a47c6d6" /><input type="hidden" name="_wp_http_referer" value="/tools/download-link-generator" />
</form>

It seems to be a server-side solution, but I don't have much information about servers and how they do it.它似乎是一个服务器端解决方案,但我没有太多关于服务器及其工作方式的信息。 I wonder how it works exactly.我想知道它是如何工作的。 Can similar projects be built for free, or do I need paid hosting and a server plus a vast knowledge of running it?可以免费构建类似的项目,还是我需要付费托管和服务器以及运行它的丰富知识? What I finally want is a link/button that will force download.我最终想要的是一个可以强制下载的链接/按钮。

Yes, it must be done server side as you need to generate a request with a header.是的,它必须在服务器端完成,因为您需要使用 header 生成请求。
You easily set up a webserver locally, especially with Node.JS .您可以轻松地在本地设置网络服务器,尤其是使用Node.JS 时


Here is how you can do it with PHP:以下是使用 PHP 的方法:

HTML form: HTML 表格:

<form action="download.php" method="post">
  <label for="url">Please enter the URL you would like to save as a file:</label>
  <input type="text" name="url">
  <button type="submit">Download</button>
</form>

download.php:下载.php:

<?php
if(isset($_POST['url'])) {
  $url = $_POST['url'];
  $file_name = basename($url);
  $file_path = $url;
  header("Content-Disposition: attachment; filename=".$file_name);
  header("Content-Type: application/octet-stream");
  header("Content-Length: ".filesize($file_path));
  readfile($file_path);
  exit;
}

This way, when the form is submitted, the contents of the text input will be sent to download.php as part of a POST request.这样,当提交表单时,文本输入的内容将作为 POST 请求的一部分发送到download.php The script checks if the $_POST array contains an element with the name url .该脚本检查 $_POST 数组是否包含名称为url的元素。 If it does, it retrieves the value of the url field, uses basename to extract the file name from the URL, sets the headers to trigger a download, and outputs the contents of the file using readfile .如果是,它会检索url字段的值,使用basename从 URL 中提取文件名,设置标头以触发下载,并使用readfile输出文件的内容。




Here is how you can do it with Node.JS:以下是使用 Node.JS 的方法:

HTML form: HTML 表格:

<form action="/download" method="post">
  <label for="url">Please enter the URL you would like to save as a file:</label>
  <input type="text" name="url">
  <button type="submit">Download</button>
</form>

Node.JS节点.JS

const express = require('express');
const app = express();
const fs = require('fs');
const http = require('http');

app.use(express.urlencoded({ extended: true }));

app.post('/download', (req, res) => {
  const url = req.body.url;
  const fileName = url.split('/').pop();
  const file = fs.createWriteStream(fileName);
  
  http.get(url, response => {
    response.pipe(file);
    file.on('finish', () => {
      file.close();
      res.setHeader('Content-Disposition', 'attachment; filename=' + fileName);
      res.setHeader('Content-Type', 'application/octet-stream');
      res.download(fileName, (err) => {
        if (err) {
          console.error(err);
        } else {
          console.log('File downloaded');
        }
      });
    });
  });
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

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

相关问题 如何创建URL以使文件可以使用HTML和PHP自动下载? - How can I create an URL to make a file downloadable automatically using HTML and PHP? 基于URL的按钮链接 - button link based on URL 如何基于URL选择器替换href链接的部分 - How can I replace parts of href link based on url selector 如何在不单击按钮的情况下自动下载文件? - How can I download a file automatically without click on button? 如何创建在.php表中打开链接的HTML按钮? - How can I create a HTML button that opens a link in a .php table? 如何为第三方 url 的文件创建下载按钮? - How can I create download button for the file from third-party url? 如何创建可下载APK文件的Intranet HTML页面? - How do I create an intranet html page that downloads an APK file? 如何创建链接到带有查询参数的按钮的 URL? - How can I create a URL that links to a button press with query parameters? 用户下载文件后如何显示Thankyou页面? - How can I display a Thankyou page after user downloads file? 我们可以为从输入 type="file" 中选择的文件创建一个自定义的 URL,该文件指向文件并在任何使用的地方下载文件? - Can we create a customized URL for the file which is selected from the input type="file" which points to file and downloads the file wherever used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM