Created
August 8, 2013 15:34
-
-
Save axeda/6185692 to your computer and use it in GitHub Desktop.
The bare minimum code to upload an image file to the StoreScaledImage.groovy script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var PLATFORM_HOST = document.URL.split('/apps/')[0]; // this is how you would retrieve the host on an Axeda instance | |
| var SESSION_ID = null // usually retrieved from login function included below | |
| /*** | |
| * Depends on jQuery 1.7+ and HTML5, assumes an HTML5 element such as the following: | |
| * <input type="file" id="fileinput" value="Upload" /> | |
| * **/ | |
| $("#fileinput").off("click.filein").on("click.filein", function () { | |
| fileUpload() | |
| }) | |
| var fileUpload = function () { | |
| $("#fileinput").off('change.fileinput') | |
| $("#fileinput").on('change.fileinput', function (event) { | |
| if (this.files && this.files.length > 0) { | |
| handleFiles("http://" + PLATFORM_HOST, this.files) | |
| } | |
| }) | |
| } | |
| var handleFiles = function (host, files) { | |
| $.each(files, function (index, file) { | |
| var formData = new FormData(); | |
| var filename = file.name | |
| formData.append(filename, file) | |
| var url = host + '/services/v1/rest/Scripto/execute/StoreScaledImage?filelabel=' + filename + "&tag=myimg" | |
| url = setSessionId(url) | |
| jQuery.ajax(url, { | |
| beforeSend: function (xhr) { | |
| xhr.setRequestHeader('Content-Disposition', filename); | |
| }, | |
| cache: false, | |
| cache: false, | |
| processData: false, | |
| type: 'POST', | |
| contentType: false, | |
| data: formData, | |
| success: function (json) { | |
| refreshPage(json) | |
| console.log(json) | |
| } | |
| }); | |
| }) | |
| } | |
| var setSessionId = function (url) { | |
| // you would already have this from logging in | |
| return url + "&sessionid=" + SESSION_ID | |
| } | |
| var refreshPage = function (json) { | |
| // here you would refresh your page with the returned JSON | |
| return | |
| } | |
| /*** | |
| * The following functions are not used in this demonstration, however they are necessary for a complete app and are found in axeda.js http://gist.github.com/axeda/4340789 | |
| ***/ | |
| function login(username, password, success, failure) { | |
| var reqUrl = host + SERVICES_PATH + 'Auth/login'; | |
| localStorage.clear() | |
| return $.get(reqUrl, { | |
| 'principal.username': username, | |
| 'password': password | |
| }, function (xml) { | |
| var sessionId = $(xml).find("ns1\\:sessionId, sessionId").text() | |
| // var sessionId = $(xml).find("[nodeName='ns1:sessionId']").text(); - no longer works past jquery 1.7 | |
| if (sessionId) { | |
| // set the username and password vars for future logins. | |
| storeSession(sessionId); | |
| success(SESSION_ID); // return the freshly stored contents of SESSION_ID | |
| } else { | |
| failure($(xml).find("faultstring").text()); | |
| } | |
| }).error(function () { | |
| $('#loginerror').html('Login Failed, please try again') | |
| }); | |
| }; | |
| function storeSession(sessionId) { | |
| var date = new Date(); | |
| date.setTime(date.getTime() + SESSION_EXPIRATION); | |
| SESSION_ID = sessionId | |
| document.cookie = APP_NAME + '_sessionId=' + SESSION_ID + '; expires=' + date.toGMTString() + '; path=/'; | |
| return true; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment