如何使用ajax创建webservice并将我的xml文件上传到我的webservice中

时间:2018-04-13 08:05:25

标签: javascript html ajax web-services

我在html页面中创建了一个按钮并读取了一个xml文件,我想将此文件上传到webservice以及如何使用ajax方法创建web服务并上传并在ajax中调用此文件。  我有点尝试 我的代码是:

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="data.js" type="text/javascript"></script>
  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
</head>
<body >
  <div>
    <form class="form-horizontal well">
      
      <fieldset>
          <label for="XMLFileInput"> <strong>XML</strong>
          </label>
          <input type="file" id="XMLFileInput" onchange="handleFiles(this.files)"
          accept=".XML">
        </div>
      </fieldset>
    </form>
    <div id="output">
    </div>
  </div>
  <br>
  <br>
  <footer>
    <input id="getFile" type="file"/><br />
<input id="displayName" type="text" value="Enter a unique name" /><br />
<input id="addFileButton" type="button" value="Upload" onclick="uploadFile()"/>
  </footer>

  
  <script type="text/javascript" src="js/read-csv.js"></script>
</body>
</html>

我的JavaScript代码是:

function handleFiles(files) {
	// Check for the various File API support.
	if (window.FileReader) {
		// FileReader are supported.
		getAsText(files[0]);
	} else {
		alert('FileReader are not supported in this browser.');
	}
}

function getAsText(fileToRead) {
	var reader = new FileReader();
	// Handle errors load
	reader.onload = loadHandler;
	reader.onerror = errorHandler;
	// Read file into memory as UTF-8      
	reader.readAsText(fileToRead);
}

function loadHandler(event) {
	var xml = event.target.result;
	processData(xml);             
}

function processData(xml) {
    var allTextLines = xml.split(/\r\n|\n/);
    var lines = [];
    while (allTextLines.length) {
        lines.push(allTextLines.shift().split(','));
    }
	console.log(lines);
	drawOutput(lines);
}

function errorHandler(evt) {
	if(evt.target.error.name == "NotReadableError") {
		alert("Canno't read file !");
	}
}

function drawOutput(lines){
	//Clear previous data
	alert("hi bagish");
	document.getElementById("output").innerHTML = "";
	var table = document.createElement("table");
	for (var i = 0; i < lines.length; i++) {
		var row = table.insertRow(-1);
		for (var j = 0; j < lines[i].length; j++) {
			var firstNameCell = row.insertCell(-1);
			firstNameCell.appendChild(document.createTextNode(lines[i][j]));
		}
	}
	document.getElementById("output").appendChild(table);
}

0 个答案:

没有答案