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 Integrating a Facebook Shop Page into Your Website

Can a Facebook shop page be integrated into a website? Any guidance on how to accomplish this task would be greatly valued. ...

Converting JSON data to XML format using Laravel

I am looking for a way to convert JSON format to XML using Laravel. Here is the expected output: <MESSAGE> <AUTHKEY>Your auth key</AUTHKEY> <SENDER>SenderID</SENDER> <ROUTE>Template</ROUTE> <CAM ...

Is there a way to showcase the JSON data within a nested array using VueJS?

Here is my Vue.js code: <script> new Vue({ el: '#feed' , data: { data: [], }, mounted() { this.$nextTick(function() { var self = this; var id = window.location.href.split('=').pop(); ...

Encountered an error when attempting to use 'appendChild' on 'Node': the first parameter is not the correct type. It was able to work with some elements, but not with others

I'm currently working on a script that utilizes fetch to retrieve an external HTML file. The goal is to perform some operations to create two HTMLCollections and then iterate over them to display a div containing one element from each collection. Here ...

The div element with the id "wrapper" is not functioning properly within the box model

I have defined an ID wrapper in CSS: #wrapper { position: relative; background: yellow; -webkit-box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2); box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2); width: 960px; padding: 40px 35px 35px ...

Is it possible for Angular.js to access a server session attribute?

I am in the process of creating an authentication system with angular.js My goal is to implement a session timeout after a period of inactivity and expire the current session if a user logs in from another device. In both scenarios - 1) idle timeout and ...

Instructions for populating a DataTable with JSON data while allowing for editing of rows

Hey there, looking for some expert advice, I've been experimenting with the datatable plugin and I'm curious if it's feasible to populate a table with row editing actions using ajax? This is the code snippet I currently have. It successfull ...

The GET API is functioning properly on Google Chrome but is experiencing issues on Internet Explorer 11

Upon launching my application, I encountered an issue with the v1/validsColumns endpoint call. It seems to be functioning properly in Chrome, but I am getting a 400 error in IE11. In IE v1/validCopyColumns?category=RFQ&columns=["ACTION_STATUS","ACTIO ...

Discover the best way to sort products by category

I'm in the process of developing an online store where customers can choose from three options: "body lotion," "body wash," and "body scrub." Once a selection is made, the corresponding product will be displayed. The products are stored in an array n ...

What is the best way to combine an additional array into a different project attribute?

There are 2 arrays being used. This is the content of userGroups: console.log(this.items) [ { "id": 63, "name": "URLGROUP-1643836551908" } ] The contents of urls are shown below: userGroup can contain ...

How to retain existing data in a SQLite database when implementing code-first onModelChange

As a newcomer to Sqlite and EF, I have created a DbContext with model classes that generate a local database when the application starts. The database is meant to be updated while the application is running. However, whenever I make changes to the model ( ...

What is the purpose of requiring the explicit invocation of app.listen(port) to enable express-ws to function properly?

I've recently started exploring NodeJS Express and came across the official tutorial from express-ws for setting up websockets in a simple project generated using npx express-generator. While following the tutorial, I noticed that in the app.js file, ...

Ways to provide input parameters to a method upon clicking an element without using the HTML onclick attribute

Is there a way to pass data between pages without redirecting to the next.php page every time a button is clicked? One suggestion is to remove the onclick attribute from the button: <button class="submit-btn"></button> and use this jQuery fu ...

Troubleshooting a problem with Angular routing when trying to access a specific URL using

My main objective is to enable users to view products by clicking on the item itself. For each product item displayed in main.html, the URL format is like this... <a href="/products/{{ product.id }}">{{ product.title }}</a> For instance, when ...

Managing assembly creation for cshtml files in ASP.NET MVC 5: Best practices

According to my understanding, the compiler for ASP.NET MVC applications produces several assemblies: All code in App_Code is compiled into its own DLL. Each individual view.cshtml file located inside the Views folder is compiled into its own separate as ...

How to effectively utilize signed requests for AWS S3 when uploading images?

My project involves developing a react native application similar to Slack, and I'm currently facing an issue with image uploads to S3. I decided to use the getSignedUrl route for this functionality. The process goes as follows: the client selects a ...

Using Ajax with Controller Action in Yii2

As a newcomer to programming, I am facing an issue where I need to call a function when the user inputs data and clicks the submit button. Although I am working with Yii2, I lack experience in Ajax. Despite my efforts, the controller action is not being tr ...

Obtain the value stored in the session

How can I hide a form based on a specific session being set? Here is my approach: <form action="" method="post" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ? ...

What is the best way to store a querystring parameter in a session?

Visitors will arrive at my website with a specific parameter, for example: or I want to save the value of this unique parameter in the Session each time it is found in the QueryString. Currently, I am using the Session_Start event in Global.asax to achie ...

Configuring timezone for 'date' type in Plotly.js

I'm currently working on a graph to showcase the trend over time. The data I have is in Unix format and I use JavaScript code (new Date(data)).toUTCString to display it in my title. Surprisingly, while the data for the graph and the title is the same, ...