2013-09-07 19:50:43 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.service");
|
2013-09-30 01:26:09 +04:00
|
|
|
ns.ImageUploadService = function () {};
|
|
|
|
ns.ImageUploadService.prototype.init = function () {};
|
2013-09-07 19:50:43 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a base64 image data to distant service. If successful, will call provided callback with the image URL as first argument;
|
|
|
|
* @param {String} imageData base64 image data (such as the return value of canvas.toDataUrl())
|
|
|
|
* @param {Function} cbSuccess success callback. 1st argument will be the uploaded image URL
|
2013-09-29 01:52:51 +04:00
|
|
|
* @param {Function} cbError error callback
|
2013-09-07 19:50:43 +04:00
|
|
|
*/
|
|
|
|
ns.ImageUploadService.prototype.upload = function (imageData, cbSuccess, cbError) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('data', imageData);
|
2013-09-30 01:26:09 +04:00
|
|
|
xhr.open('POST', Constants.IMAGE_SERVICE_UPLOAD_URL, true);
|
2013-09-07 19:50:43 +04:00
|
|
|
xhr.onload = function (e) {
|
|
|
|
if (this.status == 200) {
|
2013-09-30 01:26:09 +04:00
|
|
|
var imageUrl = Constants.IMAGE_SERVICE_GET_URL + this.responseText;
|
2013-09-07 19:50:43 +04:00
|
|
|
cbSuccess(imageUrl);
|
|
|
|
} else {
|
|
|
|
cbError();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formData);
|
|
|
|
};
|
2013-09-30 01:26:09 +04:00
|
|
|
})();
|