1

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;
        }

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>

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

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

thanks.

zosim
  • 2,959
  • 6
  • 31
  • 34
  • possible duplicate of [Using Jquery and Ajax to save a file in ASP.Net](http://stackoverflow.com/questions/3111750/using-jquery-and-ajax-to-save-a-file-in-asp-net) – Quentin May 27 '13 at 09:56
  • See also [this](http://stackoverflow.com/questions/15637758/django-invoking-browsers-save-as-from-an-ajax-invoked-view), [this](http://stackoverflow.com/questions/10256050/generate-save-as-dialog-box-with-ajax-request-in-ext-js) and [this](http://stackoverflow.com/questions/7684297/content-disposition-with-attachment-doesnt-work-for-ajax-response) – Quentin May 27 '13 at 09:57
  • See also: http://stackoverflow.com/questions/13405129/javascript-create-and-save-file – Menelaos May 27 '13 at 10:03
  • In this situation classic form would be better. – Michas May 27 '13 at 10:10
  • Hmm try force the file to be `application/octet-stream` mime type? It will then download. The problem is that you CAN'T force 'save as' if the person checked option to memorize the download preference (like FF). – RaphaelDDL May 27 '13 at 21:06

1 Answers1

0

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.

zosim
  • 2,959
  • 6
  • 31
  • 34