I want to read my uploading xml file and show it in a TextArea from the client-side. I am facing problem in my javascript code where I am using string variable instead of url. Is it not correct way to use url? If not how can I use it? Most of the query I have found that they are using fixed file as url but my file can be changed. Here is my code:
function fillTextArea() {
var text = document.getElementById("connectionName").value;
document.getElementById("recentDevices").value += text + '\n';
var filename = $('input[type=file]').val().split('\\').pop();
var xml = new XMLHttpRequest();
xml.open('Get', filename, false);
xml.send();
var xmlData = xml.responseXML;
document.getElementById("xmlFileInfo").value += xmlData;
}
<div class="row">
<div class="col-md-6">
<div class="input-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">Connect To:</span>
<input type="text" class="form-control" id="connectionName" name="connectionName" placeholder="Name" aria-describedby="basic-addon1">
</div>
<table>
<tr>
<td>Upload XML File</td>
<td><input type="file" name="xmlFile" id="xmlFile" accept=".xml" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="upload" class="btn btn-primary" onClick="fillTextArea()" /></td>
</tr>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Recent Machines</h3>
</div>
@Html.TextArea("Xml File Information", new { id = "xmlFileInfo", style = "max-width:100%; min-height:250px", @class = "form-control" })
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Recent Devices</h3>
</div>
@Html.TextArea("Recent Devices", new { id = "recentDevices", style = "max-width:100%; min-height:250px", @class = "form-control" })
</div>
</div>
</div>
I think my code has a problem in this line below,
xml.open('Get', filename, false);
XMLHttpRequestto make calls to the server, not to read a file on the client. You can use theFileReaderto read a file directly on the client using JS. Note that if you need to store the file, then you would still have to upload it to your server