Deliver XML document to client from an ASP.NET MVC webpage

I need help with an ASP.NET MVC web page that requires user input to create an XML file using a javascript function. After the user enters information and clicks a button, how can I display the XML created by the javascript method?

In the .cshtml file: For example, let's consider a text box.

<div class="col-md-8">
    <input id="NameTextBox" name="Name" type="text" placeholder="Enter a name ..." class="form-control" required="required" autofocus="autofocus" />
</div>
<input id="CreateXml" type="submit" class="btn" value="Create XML" onclick="javascript:createXml()" />

In the .js file:

function createXml() {

    var returnXml = "<Hello>" + "\n";
    var name = $('#NameTextBox').text();
    returnXml = returnXml + name + "\n" + </Hello>;

}

How can I display the 'returnXml' string in a file on the client side? Is it possible to achieve this without posting to an Action (possibly by using ajax)? Any suggestions are appreciated.

Answer №1

UPDATE:

After some trial and error, I successfully achieved the desired outcome using the following code snippet:

window.navigator.msSaveOrOpenBlob(new Blob([returnXml], { type: "text/xml" }));

This code triggers a dialog that allows the user to save or open the file with a unique GUID name. Additionally, it is possible to specify a custom file name by passing it as a parameter to the msSaveOrOpenBlob method.

(Please note that this solution is compatible with IE11, further testing is required to confirm support for other browsers.)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for periodically updating an asp.net mvc view using signalR?

Currently, I am using periodic Ajax calls from the view to fetch new data every 2 seconds. The controller is responsible for monitoring hardware/sensor signals. Is it possible to implement SignalR instead of Ajax? I have come across examples where change ...

Creating objects based on interfaces

After looking at this straightforward code: interface int1 { aa: string, bb: number, } const obj1:int1 = {} //#1 function fun(param_obj:int1) { //#2 } I am curious as to why the compiler throws an error: Type '{}' is missing the fol ...

Crafting LayerGroups on the Fly with Leaflet

Dynamic Creation of LayerGroups: Is it Achievable? I am currently working on a web map showcasing the various tree species present in parks. My goal is to create a separate LayerGroup for each species so that users can toggle visibility using a LayerContro ...

Error: The property 'ss' cannot be accessed because it is undefined

Our main source page will be index.html, while Employees.html is where our results end up. An error occurred: TypeError - Cannot read property 'ss' of undefined Error in the code: let rating = req.body.ss; Seeking assistance please >< C ...

The calendar fails to display events once the $scope.splice(0) and $scope.push(event) functions are executed

Greetings, I am encountering an issue with the angular ui-calendar (angular wrapper for arshaw's fullcalendar). Initially, I load the events using a $http call in the first step and the calendar displays correctly. However, in the next step, when lo ...

Sending an AJAX request to an external URL using PHP

I've been attempting to execute an ajax post request towards an external URL, and while the same request works fine in hurl.it, it doesn't seem to provide the expected output when I try it directly in the browser. When using hurl.it, I input the ...

How to trigger a horizontal scroll on load for a specific ID in HTMLDivElement

Looking for advice on how to make one of the horizontally scrolling DIV containers on a site scroll to a specific position onLoad. The challenge is that this particular container is located far down the page vertically, and we want it to scroll horizontall ...

Access Flask variable in JavaScript code

Currently working on a CTF challenge, my query is not seeking assistance in solving it, but rather pertains to syntax. The task involves retrieving the secret key from Flask server's configuration. This key is stored within the app.secret_key variable ...

The file always fails the Regex test in Node.js

I am dealing with a log file that contains lines structured like this: [Thu Mar 30 2017 11:24:51 GMT+0100 (WEST)] {"serial":"CA-2M-1107619","type":"iface","body":{"action":"up","device":"tun_man","ip":"127.255.0.10","ip6":"2016:900d:c0de::1001"} My goal ...

Getting Started with NPM Package Initialization in Vue

I'm attempting to incorporate the v-mask package into my Vue project using npm. Following the documentation, I executed npm install v-mask, but I am unsure where exactly to initialize the code. I tried placing it in the main.js file: import { createAp ...

Trouble with an external .js script

function displayMessage() { var isConfirmed = confirm("Do you want to proceed?"); if (isConfirmed) { window.location = "./proceed"; } }; function checkIfEmpty(){ console.log("checkIfEmpty"); }; @CHARSET "ISO-8859-1"; form { margin:0 auto; width:300px ...

What is the process for obtaining the updated file name of a file that has been saved using ajax AsyncFile

When utilizing the asyncfileupload ajax plugin from the ajax toolkit to save a file, I am running into an issue where the filename needs to be changed to prevent duplicates. To inform the user of the new filename after upload, I have implemented the follo ...

"Centered in the middle of the screen, an animated

Encountering an issue with this code on Chrome. Clicking on the checkbox causes the text/checkbox to shift in position/padding/margin unexpectedly.. :( View the code on JSFiddle HTML <input id="inp" type="checkbox" /> <div id="cb" class="inp_ch ...

Assign the ngClick event handler to the capturing phase

Can the ngClick event handler be configured to work in the capturing phase, as discussed in this informative article? I am interested in stopping events from propagating down to child elements and then back up again when a specific condition is met for t ...

Is it possible to modify the inactive color of just one radio button in Framework7?

Is there a way to change the inactive color of only one radio button in Framework7? I am aware that using the CSS variable --f7-radio-inactive-color allows me to set the inactive color for all radio buttons. However, I specifically want to modify the inac ...

"The Promise in the AngularJS Karma test specification did not resolve and the .then() method was not invoked

An issue arises when attempting to perform AngularJS Karma Unit Testing on a service. The service includes a method like the one below: service.getIntersectingElements = function (element, elements) { var deferred = $q.defer(); var tolerance = 20 ...

Displaying an error message prior to submitting the form in an AngularJS form validation process

I have implemented form validation using angularjs, but I am encountering an issue where the error message is displayed upon page load instead of after an actual error. I have set up a validation summary using a directive. Can you please advise me on how t ...

How to adjust cell alignment in Handsontable

Handsontable showcases cell alignment options in the align cell demo: Horizontal Left Center Right Justify Vertical Top Middle Bottom Displayed below is a screenshot: To retrieve data, utilize the following code snippet: $('#table-wrapper&ap ...

Tips on validating file types using jQuery's Ajax function

I am new to web programming and need help validating and uploading a file. I want to have 2 conditions: if the file type doesn't match, show an alert saying "Invalid file"; second condition, if the file is empty or matches, insert data. However, if th ...

Prevent mouse over scrolling for every <select> element on a webpage using JQuery

My code seems to be having some issues. Can you help me figure out what's wrong? <script type="text/javascript" language="javascript"> //trying to disable scrolling on mouse over of <select> elements $(document).ready(function() { ...