Importing Files into the CMS
Your can use your custom web page to import files into the SmartServer CMS. The same files that can be imported using the CMS web pages (i.e., Devices widget and Device Types widget) can also be imported using the custom web page. The custom web page code allows you to import .dtp, .zip, .xif, .bac, .mod, .btm, .dtd, devices.csv, and other file types. To import the files, you will need to know the SmartServer user customerId, which can be found by using the /iap/users/current GET request.
The example source code shown below is a subset of the code that enables a custom web page to import a file. This example does not support offline license activation (license import) since the request URI and payload data formats are different.
<html> <body> <input type="file" id="importFile" name="myfile" onchange="SendImportFile()"> <body> <script> var customerId = -1; function getCustomerId(mode) { var url; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 20O) { // Got response customerId = xhr.response.customerId if(mode === 1) SendImportFile(); } else if(xhr.readyState == 4) { //error } url = "https://10.0.0.229/iap/users/current"; xhr.open("GET", url, true); xhr.send(); } function SendImportFile() { var formData = new FormData(); var fileObj = document.getElementById("importFile").files[0]; var filename = fileObj.name; var str = "{\"fileName\":\"" + filename + "\"}"; var blob = new Blob([str],{type:'application/json'}); var xhr = new XMLHttpRequest(); if(customerId === -1) { getCustomerId(1); return; } xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 204) { // Write successful } else if (xhr.readyState == 4 && xhr.status == 428) { // Sometimes need to confirm whether to import file - confirmation ID is included in response if(confirm("Are you sure you want to import new device files\r\n\r\n" + filename)) { SendImportFileConfirmation(xhr.response.confirmationId); } } else if(xhr.readyState == 4 && xhr.status == 401) { //login(); } else if(xhr.readyState == 4) { //error } xhr.open("POST", "https://10.0.0.229/iap/metafile?customerId=" + customerId, true); formData.append("jsonFile", blob); formData.append("metaFile", fileObj, filename); xhr.send(formData); } function SendImportFileConfirmation(confirmationId) { var url; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 204) { // Write successful } else if(xhr.readyState == 4) { //error } url = "https://10.0.0.229/iap/metafile/confirm?"confirmationId=" + confirmationId + "&customerId=" + customerId; xhr.open("POST", url, true); xhr.send(); } </script> </html>
Example status codes that may be returned are described in the table below.
Status Code | Description |
---|---|
204 | Indicates that the request was successfully sent. |
400 | Indicates an invalid request. |
415 | Indicates that the Content-Type header needs to be multi-part/form-data. Other values can also return a 415 status code. |
428 | For a .zip file, indicates that the .zip file may already have been downloaded. In this case, you need to send another POST request with the confirmationID that is included in the response in order to overwrite the previous files. |