aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.WebDashboard/ApiClient.js
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-05-04 17:20:27 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-05-04 17:20:27 -0400
commit00222c84935ea9c81a997b12bafa6cf5f13c7b91 (patch)
tree849e3af10eac7f768b848741a80abbc0af3afaab /MediaBrowser.WebDashboard/ApiClient.js
parent055a6eb4cc183cf84dc6b72196cd599c9c471695 (diff)
add basic ability to upload images for items
Diffstat (limited to 'MediaBrowser.WebDashboard/ApiClient.js')
-rw-r--r--MediaBrowser.WebDashboard/ApiClient.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js
index 84f34e681..2e11a72fc 100644
--- a/MediaBrowser.WebDashboard/ApiClient.js
+++ b/MediaBrowser.WebDashboard/ApiClient.js
@@ -839,6 +839,63 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
return deferred.promise();
};
+ self.uploadImage = function (itemId, imageType, file) {
+
+ if (!itemId) {
+ throw new Error("null itemId");
+ }
+
+ if (!imageType) {
+ throw new Error("null imageType");
+ }
+
+ if (!file) {
+ throw new Error("File must be an image.");
+ }
+
+ if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
+ throw new Error("File must be an image.");
+ }
+
+ var deferred = $.Deferred();
+
+ var reader = new FileReader();
+
+ reader.onerror = function () {
+ deferred.reject();
+ };
+
+ reader.onabort = function () {
+ deferred.reject();
+ };
+
+ // Closure to capture the file information.
+ reader.onload = function (e) {
+
+ var data = window.btoa(e.target.result);
+
+ var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
+
+ self.ajax({
+ type: "POST",
+ url: url,
+ data: data,
+ contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
+ }).done(function (result) {
+
+ deferred.resolveWith(null, [result]);
+
+ }).fail(function () {
+ deferred.reject();
+ });
+ };
+
+ // Read in the image file as a data URL.
+ reader.readAsBinaryString(file);
+
+ return deferred.promise();
+ };
+
/**
* Gets the list of installed plugins on the server
*/