重定向网页到文件 [英] Redirecting webpage to a file

查看:104
本文介绍了重定向网页到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设presents表,但有一个选项,以下载为Excel表的网页。该选项psented作为提交表单按钮$ P $(包含所需参数来创建相同的报告,但Excel文件)。

Suppose a webpage that presents a table, but with an option to download it as an Excel sheet. The option is presented as a button that submits a form (containing the required parameters to create the same report, but for Excel).

<form action="makereport.blah">
<!-- Some parameters -->
<input type="button" value="Export to Excel" onClick="submit();"/>
</form>

最后一步仍然是浏览器直接到创建的Excel表。

The final step remains to direct the browser to the created Excel sheet.

我看到三种可能性:


  1. 在操作页面的形式,用OnLoad事件:URL重写在JavaScript中创建的文件路径,并重新加载页面

  1. In the "action" page of the form, with the OnLoad event: rewrite in JavaScript the URL to the created file path and reload the page

在HTTP头:重定向到文件的URL(使用重定向命令)

In the HTTP header: Redirect to the url of the file (with the Redirect command)

使用 IFRAME ,其中的src 是目标文件

那些看起来有点的的hackish 的,但我已经在过去使用它们。 如何是这样做正常?

Those seem a bit hackish, but I've used them in the past. How is this normally done?

(作为一个说明,我使用生成HTML语言(和Excel表)是一个内部的解决方案。所以,如果PHP有东西,我只想有移植)。

(As a note, the language I'm using to generate the HTML (and Excel sheet) is an in-house solution. So if PHP has something for that, I would just have to port it).

推荐答案

脚本/ code处理表单(服务器端)的发布可以生成Excel的小号preadsheet。然后,而不是与一个HTML响应(或某种重定向)回答,则可以改为直接返回在S preadsheet(与将如果文件直接加载要发送的正确的HTTP标头)。

The script/code that handles the posting of the form (server side) can generate the Excel spreadsheet. Then instead of replying with an HTML response (or some kind of redirect) you can instead return the spreadsheet directly (with the correct HTTP headers that would be sent if the file was loaded directly).

在PHP的一个例子:

<?php

// generate spreadsheet in file
$file = do_something_to_generate_spreadsheet();

// specifies it is an excel spreadsheet response
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . $file . '"'); // filename
header('Content-Length: ' . filesize($file)); // length

readfile($file); // sends file to client

?>

这篇关于重定向网页到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆