0

I need help to accomplish this task: download an XML file (with the prompt to select a path and a name of the file) using Jquery to receive an XML from the backend (asp MVC). Tried to remove [HttpPost], don't return any view, and I don't have more ideas to try. I researched a lot and found some solutions but none works. When I executed this code nothing happen. Some ideas? EDITED: The steps I want to do when I click a button, open the prompt, write a name and select a path and download the file in the browser.

I have this javascript:

 $('#exportButton').on("click", function () {                
            var clientName = $(actionsButtons).data('clientname');
            var currentHCSystem = $(actionsButtons).data('hcsystemname');
            var clientConfig = false;
            var fileConfig = false;
           
            // Create FormData object  
            var parameters = new FormData();
            parameters.append("clientName", clientName);
            parameters.append("currentHCSystem", currentHCSystem);

                       
            $.ajax({
                url: $(actionsButtons).data('export'),
                type: 'POST',
                data: file,
                cache: false,
                processData: false,
                contentType: false,
                success: function (response) {
                    //??                  
                },
                error: function (err) {
                    //??
                }
            });

        });

and MVC

        [HttpPost]
    public ActionResult ExportFiles(string clientName, string currenthcsystem)
    {
        try
        {
            var systemActionsConfigurationData = service.GetHcSystemActionsConfigurationData(currenthcsystem);
                           
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename = itfunda.xml");    
            Response.ContentType = "text/xml";

            var serializer = new XmlSerializer(systemActionsConfigurationData.GetType());
            serializer.Serialize(Response.OutputStream, systemActionsConfigurationData);
            return View("Index");


        }
        catch (Exception)
        {

            throw;
        }

    }

1 Answers1

0

Unless I'm not following correctly, it looks like you've got a procedural problem, not a technical one (or, maybe in addition to). It sounds like you're being asked to export XML to a file on the server, not returning XML data via AJAX, right?

Since the server won't prompt you for a file/path, you will need to provide that via params in the AJAX call, which means you'll have to prompt for the path prior to making that call.

If it's the other way around, then you'll need to return the data from the server with several headers in the response:

The combination of these will tell the client:

  • this data is of a certain type, so the client knows how to handle/parse/process it
  • the data is intended to be downloaded to a file, the name of which is filename="..."

Also FWIW, I prefer application/xml (see RFC 7303 or MDN)

For more help with AJAX, there are lots of articles on the Interwebs, such as this one that can help with particular details if you get stuck.

kerry
  • 757
  • 6
  • 16
  • Hi Kerry, thanks for the reply. I need the logic to open the prompt, and when is accepted download the xml file, all on the browser side. Only go to the server-side to get the business object and serialize it. I should only return the xml from the backend and make all the logic including the prompt? – Gonzalo Maciel Jun 08 '22 at 22:44
  • I mean on the front end side after the ajax call? – Gonzalo Maciel Jun 08 '22 at 22:51
  • The easiest way to accomplish the download is to modify the response headers. Adding "Content-Disposition" with "filename=" will force the browser to download the file. If you want to do that interactively, then you need to add the data to a DOM object and export it. See the last link for details: https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – kerry Jun 09 '22 at 21:28