自动从 HTML 传输文件

发布于 2024-07-09 14:09:00 字数 518 浏览 5 评论 0原文

我正在构建一个 Web 应用程序,指导用户完成应用程序的配置和安装。 它动态构建一组配置文件,然后将它们与应用程序的安装程序一起以存档(.ZIP 文件)的形式发送。 该网页是从 Linux shell 脚本生成的(抱歉),出于安全原因,我希望直接从脚本发送文件,而不是作为链接发送,因此用户无法直接访问它。

过程如下:用户输入一些信息并且生成文件后,我想显示一个包含说明的页面,然后自动开始下载,而不要求用户单击“下载此文件”链接:

#!/bin/bash
echo_header_and_instructions         # Standard HTML
<Magic HTML tag to start transfer>   # ??? What goes here???
command_to_stream_the_files          # Probably 'cat'
echo_end_tags                        # End the page.

谢谢你的帮助!

I'm building a web application that guides my users through the configuration and installation of an application. It builds a set of configuration files dynamically, then sends them in an archive (.ZIP file) along with an installer for the application. The web page is generated from a linux shell script (sorry), and for security reasons, I'd prefer the file be sent directly from the script, rather than as a link, so the user can't access it directly.

Here's the process: Once the user has entered some information, and the files have been generated, I want to display a page with instructions, then start the download automatically, without asking the user to click a "download this file" link:

#!/bin/bash
echo_header_and_instructions         # Standard HTML
<Magic HTML tag to start transfer>   # ??? What goes here???
command_to_stream_the_files          # Probably 'cat'
echo_end_tags                        # End the page.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜清冷一曲。 2024-07-16 14:09:00

我认为你真的不能这么做。
您可以通过以下标头强制下载文件,但据我所知,您不能混合 HTML 和文件下载。

标题:

Content-type: MIME/type
Content-Disposition: attachment; filename="archive.zip"
Content-Length: filesize_in_bytes

内容长度不是强制性的,但使用它可以确保下载对话框可以显示还有多少文件需要下载。

您可以做的一件事是使用 javascript 重新加载页面。 因为该页面是文件下载,所以原始 HTML 页面将保留在原处:

<html>
<head>
<title>This is the page containing information</title>
<script type="text/javascript">
window.onload = function(){
 document.location = 'somefile.zip';
}
</script>
</head>
<body>
This page will stay visible while the file is being downloaded<br>
</body>
</html>

You Can't really do that I think.
You can force the file download through the following headers but as far is I know you can't mix HTML and file download.

Headers:

Content-type: MIME/type
Content-Disposition: attachment; filename="archive.zip"
Content-Length: filesize_in_bytes

The content length is not mandatory but using it will make sure that the download dialog box can show how much more file there is to download.

A thing you could do is reload the page using javascript. Because the page is a file download the original HTML page will stay in place:

<html>
<head>
<title>This is the page containing information</title>
<script type="text/javascript">
window.onload = function(){
 document.location = 'somefile.zip';
}
</script>
</head>
<body>
This page will stay visible while the file is being downloaded<br>
</body>
</html>
晒暮凉 2024-07-16 14:09:00

我认为您可以使用 元标记 让浏览器提示用户下载文件进行刷新,但正如 Pim Jager 所说,我认为您无法通过一次传输来完成此操作。 你也许可以尝试做类似的事情:

<meta http-equiv="refresh" content="5;url=http://example.com/pathtodownload.zip" />

I think you can make the browser prompt the user to download a file by using the meta tag to do a refresh, but as Pim Jager said I don't think you can do it with one transfer. You could maybe try doing something like:

<meta http-equiv="refresh" content="5;url=http://example.com/pathtodownload.zip" />
慕烟庭风 2024-07-16 14:09:00

一位朋友提供了魔法:使用 标签创建一个仅包含要下载的文件的“不可见”框架:

<iframe id="file_download" width="0" height="0" scrolling="no" 
   frameborder="0" src=/cgi-bin/my/scripts/sendfiles?file=$filename.zip>
   You need a browser that supports frames.
</iframe>`

A friend supplied the magic: use an <iframe> tag to create an "invisible" frame that contains only the file to download:

<iframe id="file_download" width="0" height="0" scrolling="no" 
   frameborder="0" src=/cgi-bin/my/scripts/sendfiles?file=$filename.zip>
   You need a browser that supports frames.
</iframe>`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文