JavaScript:如何“缓存”要下载的文件,然后根据请求触发下载? [英] JavaScript: How to "cache" a file to download, then trigger download upon request?

查看:108
本文介绍了JavaScript:如何“缓存”要下载的文件,然后根据请求触发下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,允许用户生成可以下载的HTML电子邮件。当用户点击预览按钮时,表单被提交到一个新的窗口,它实际上生成了html。

I have a web app that allows a user to "generate" an HTML email that they can download. When the user clicks the "preview" button, the form is submitted to a new window, which actually generates the html.

<form action="/preview" method="post" target="previewWindow" onsubmit="window.open('', 'previewWindow', 'width=660,height=800,status=yes,resizable=yes,scrollbars=yes')">
    <!-- form elements go here -->
</form>

然后我在页面上显示HTML以便在实际下载文件之前预览html 。

Then I display the HTML on the page in order to "preview" the html before actually downloading the file.

<!-- this is the html used in the popup window -->
<html>
<head>
    <title>Email Preview</title>
</head>
<body>
    <div id="header">
        <a href="#">Download</a>
    </div>
    <div id="emailHtml">
        <?php echo $this->emailHtml; // the HTML that I will want to download soon ?>
    </div>
</body>
</html>

我的脚本已经完成了生成HTML的工作,因为我已经在页面上显示了它。有没有办法从这个HTML生成一个HTML文件并缓存它,以便当用户点击下载链接时,它只是触发浏览器下载这个内容作为一个文件,而不必重新生成HTML?我试图不保存服务器上的任何文件,如果我不需要的话。

My script has already done the work of generating the HTML since I am already displaying it on the page. Is there a way to generate an HTML file from this HTML and "cache" it so that when the user clicks the download link, it simply triggers the browser to download this content as a file without having to re-generate the html again? I'm trying not to save any files on the server if I don't need to.

推荐答案

基于Mikko Ohtamaa的评论上面,似乎没有办法用JavaScript实现这一点。我已决定将该文件保存到临时目录中作为解决方法。我仍然有兴趣知道是否有办法做到这一点,而不需要将文件保存到服务器。

Based on Mikko Ohtamaa's comment above, it seems that there is no way to achieve this with JavaScript. I have decided to save the file to a temp directory as a workaround. I'm still interested to hear if there is a way to do something like this without saving the file to the server.

这篇关于JavaScript:如何“缓存”要下载的文件,然后根据请求触发下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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