另存为文本文件 JavaScript
目前,我正在使用这段代码:
<?php
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$_POST['savename'].'.bob"');
echo($_POST['savedata'])
?>
它获取表单的数据,创建附件并将其发送回来。这是没有用的,因为客户端有数据。我想将其重写为:
myForm.onsubmit = SomeSaveToFileFunction()
Currently, I'm using this code:
<?php
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$_POST['savename'].'.bob"');
echo($_POST['savedata'])
?>
It gets the data of a form, creates an attachment and sends it right back. This is useless, because the client has the data. I would like to rewrite it to:
myForm.onsubmit = SomeSaveToFileFunction()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Chrome 现在可以做到这一点。
html
Javascript
演示
http://jsfiddle.net/88BYB/ 3/
编辑最新版本的chrome不接受下载属性。(chrome35)
Chrome can do that now.
html
Javascript
DEMO
http://jsfiddle.net/88BYB/3/
EDIT the latest version of chrome does not accept the download attribute.(chrome35)
事实上,流量可能毫无用处。你说得对。但正如 Pointy 所说,不幸的是,仅仅使用 javascript 是没有办法的。
如果您只想以更优雅的方式实现下载(而不是页面重新加载),请使用加载 PHP 响应的“隐藏”iframe。也许您可以轻松地将 PHP 处理从 POST 切换为 GET 并且...
您可能需要一些代码来抑制对表单的默认处理...我不知道。
The traffic might be useless, indeed. You're right. But like Pointy said, unfortunatelly there's no way by simply using javascript.
If you would just like to have your download implemented in a more elegant way (than page reload), make use of a "hidden" iframe, that loads the PHP-response. Maybe you could easily switch the PHP processing from POST to GET and ...
You'll propably need some code to suppress your default handling of the form... which I don't know.
出于明显的安全原因,JavaScript 无法直接在本地计算机上写入文件。然而,您也许可以使用 ActiveX 对象来完成您需要的操作,但同样,它仅适用于 IE。然后,您将需要 NPAPI 等等效项来使其与 Chrome 兼容。
我想你应该坚持你实际做的方式,如果它像你想要的那样工作。
编辑:我的意思是 NPAPI 而不是“API”。它可以让您构建以 ActiveX 相同方式工作的插件,并让您执行相同的操作。
JavaScript cannot directly write files on a local computer, for obvious security reasons. However, you might be able to do just what you need using ActiveX object, but once again, it will only work with IE. You would then need equivalents like NPAPI to make it compatible with Chrome for example.
I guess you should stay with the way you actually do it, if it works like you want it to.
EDIT: I meant NPAPI and not "API". It lets you build plug-ins that work (kind of) the same way ActiveX do, and lets you do the same things.