从javascript打开另存为对话框 [英] open save as dialog from the javascript

查看:263
本文介绍了从javascript打开另存为对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现以下情况:我有一个html页面,其中是一个按钮和一些文本框.当我单击按钮时,我想从文本框数据中创建一个xml并将该xml发送到服务器,然后服务器将这个xml作为附件返回,我将有一个另存为"对话框,并且可以将该xml保存在本地.我有以下将XML发送到服务器的jquery函数:

I need to realize the following scenario: I have a html page, where is a button and some textboxes. When I click the button i want to create an xml from the textboxes data and send this xml to the server, than server returns this xml back as an attachment and I will have a Save As dialog and I can save this xml locally. I have the following jquery function which send the xml to the server:

        function GetXml() {
            var xmlData = '{"xml": "<test>test</test>"}';
            var contentType = "application/json";
            var eDoc = "";
            $.ajax({
                type: 'POST',                         
                url: 'http://localhost/xmlservice.svc/rest/GetXmlToSave',
                data: xmlData,
                contentType: contentType,
                success: function (result) {                        
                    alert(result);
                },
                error: function (result) {                      
                    alert(result);
                },
                async: false,                   
            });
            return result;
        }

但是我不知道从服务器使用返回的xml来强制从javascript强制执行另存为对话框.

But i don't know hot to force Save As dialog from javascript with the returned xml from the server.

我可以使用经典的提交按钮来实现它:

I can realize it with the classic submit button:

<form action='http://localhost/xmlservice.svc/rest/GetXmlToSave' method="POST" runat="server" >                                
            <input type="submit"/>
        </form>

但是在这种情况下,我无法在客户端上创建xml.

But in this scenario I cannot create xml on the client side.

是否可以通过javascript强制执行另存为"对话框?

Is it possible to force Save As dialog from the javascript?

谢谢.

推荐答案

根据回答和评论,我决定使用经典表单提交.我将所有表单提交给服务器,而xml将在服务器端构建. 建议的解决方案看起来非常好,但是我的目标生产环境存在一些限制.例如.无法安装任何外部组件(例如闪光灯).该解决方案也必须在较旧的IE 8浏览器中也可以工作(我不能使用所有新的HTML 5功能=>我不能使用jsfiddle).看来,经典的javascript不支持另存为对话框,尤其是出于安全原因.

From the answers and comments I have made a decision to use classic form submit. I will submit all form to the server and the xml will be constructed on the server side. Some of suggested solutions looks very good, but there are some restrictions in my target production environment. E.g. there is not possible to install any external components (e.g. flash.). the solution must work also in the older IE 8 browser (i cannot use all new HTML 5 features => i cannot use jsfiddle ). And it seems, that classic javascript doesn't support save as dialog especially from the security reasons.

这篇关于从javascript打开另存为对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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