从HTML自动传输文件

时间:2008-12-07 17:33:58

标签: html linux scripting file-transfer

我正在构建一个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.

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

我认为你不能真的这样做 您可以通过以下标题强制下载文件,但据我所知,您不能混合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>

答案 1 :(得分:1)

我认为你可以让浏览器通过meta tag提示用户下载文件进行刷新,但正如Pim Jager所说,我不认为你可以通过一次转移来实现。您可以尝试做类似的事情:

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

答案 2 :(得分:0)

一位朋友提供了魔法:使用<iframe>标签创建一个“隐形”框架,其中只包含要下载的文件:

<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>`