由 Javascript 触发的“保存”窗口 - 如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3529081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:25:06  来源:igfitidea点击:

"Save" window triggered by Javascript - HOW?

javascripthtml

提问by Mubeen

how to triggered open/save window by using javascript?

如何使用javascript触发打开/保存窗口?

回答by Darin Dimitrov

Depending on what you are trying to save this may or may not be feasible. If you want to trigger the Save dialog to save the current page you could try document.execCommand("SaveAs")but not sure if it will work cross browser. If you want to allow the user download a file then create a link that the user clicks and the server sends the file along with the proper HTTP headers:

根据您要保存的内容,这可能可行,也可能不可行。如果您想触发“保存”对话框以保存当前页面,您可以尝试document.execCommand("SaveAs")但不确定它是否可以跨浏览器工作。如果您想允许用户下载文件,则创建一个用户单击的链接,服务器将文件连同正确的 HTTP 标头一起发送:

Content-Disposition: attachment; filename=foo.txt

回答by Pekka

I assume you mean the "Open page" and "Save page as..." dialogs.

我假设您的意思是“打开页面”和“将页面另存为...”对话框。

These cannot be triggered by JavaScript from within an HTML page.

这些不能由 HTML 页面中的 JavaScript 触发。

You can, however, generate downloadable files from within JavaScript using the Flash-based Downloadifyhelper. That way, you could theoretically generate a file download in the browser that contains the current page's HTML source code.

但是,您可以使用基于 Flash 的Downloadify帮助程序从 JavaScript 中生成可下载文件。这样,理论上您可以在浏览器中生成包含当前页面 HTML 源代码的文件下载。

回答by Gordon

Internet Explorer supports triggering Browser Commands:

Internet Explorer 支持触发浏览器命令

document.execCommand("SaveAs");

would open the SaveAs Dialog. I am not aware of a Crossbrowser way of doing this.

将打开另存为对话框。我不知道这样做的跨浏览器方式。

回答by Marcello Faga

Using simple Javascript I think it is no possible to open a file window.

使用简单的 Javascript 我认为不可能打开文件窗口。

Then I'd like to propose you the jQuery "Custom File Input" functionality.

然后我想向您推荐 jQuery“自定义文件输入”功能。

It gives you the possibility to choose a file on the client file system, it is not a real open/save window, but it can help you to build some file system interoperability function.

它使您可以选择客户端文件系统上的文件,它不是一个真正的打开/保存窗口,但它可以帮助您构建一些文件系统互操作性功能。

Download the jQuery library and then the file input plugin

下载 jQuery 库,然后下载文件输入插件

http://plugins.jquery.com/files/jquery-custom-file-input.js.txt

http://plugins.jquery.com/files/jquery-custom-file-input.js.txt

Example:

示例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>

<script type="text/javascript" src="./JQuery/jquery.js"></script>
<script type="text/javascript" src="./JQuery/jquery-custom-file-input.js"></script>


<script type="text/javascript">
<!--
    $().ready(function() 
{
    $("#filewindow").file().choose(function(e, input) {
        alert("you choose: " + input.val());
    });

});
// -->
</script>


<button id="filewindow">File</button>


</body>
</html>