HTML and CSS Reference
In-Depth Information
ShowPreview() creates a FileReader object. Because XML files are essentially text files, the
readAsText() method is used to read the files. The load event handler of the FileReader accesses the XML
file content using the result property. The XML file may be very large, and for preview purposes only part
of the file (up to 500 characters) is extracted. The title attribute of the underlying anchor element is then
set to the extracted XML data.
The HidePreview() function doesn't do anything special in this case because the browser
automatically hides the tooltip when mouse pointer leaves a hyperlink under consideration.
The task of uploading the XML files to the server is accomplished by the UploadFiles() function,
shown in Listing 9-30.
Listing 9-30. UploadFiles() Function
function UploadFiles() {
var data = new FormData();
for (var i = 0; i < files.length; i++) {
if (files[i].type == "text/xml") {
data.append(files[i].name, files[i]);
}
}
$.ajax({
type: "POST",
url: "/Upload/UploadFiles",
contentType: false,
processData: false,
data: data,
success: function (result) {
$("#errors").empty();
$("#errors").html(result);
},
error: function () {
alert("There was error uploading iles!");
}
});
}
UploadFile() iterates through the selected files and appends only the XML files to a FormData object.
The $.ajax() method then makes a POST request to the UploadFiles() action method of the Upload
controller. The contentType and processData options are set to false as before. If there are any schema
validation errors, they're displayed in a <div> element.
The UploadFiles() action method that saves and validates the XML files is shown in Listing 9-31.
Listing 9-31. UploadFiles() Action Method
[HttpPost]
public JsonResult UploadFiles()
{
if (Request.Files.Count > 0)
{
HttpFileCollectionBase files = Request.Files;
foreach (string key in files)
{
HttpPostedFileBase file = files[key];
 
Search WWH ::




Custom Search