Last active
October 13, 2015 12:01
-
-
Save shaoner/cad3dce08c270ce10b89 to your computer and use it in GitHub Desktop.
Ionic upload from base64 encoded image
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
angular.module('myapp.image', [ ]) | |
.factory('Image', function ($http) { | |
function blobFromDataURI(dataURI) { | |
var raw = window.atob(dataURI.replace(/[^,]+,/, '')), | |
imgBuffer = []; | |
for (var i = 0, len = raw.length; i < len; i++) { | |
imgBuffer.push(raw.charCodeAt(i)); | |
} | |
return new window.Blob([ new Uint8Array(imgBuffer) ], | |
{ type: 'image/jpeg' }); | |
} | |
function formData(name, dataURI) { | |
var fd = new window.FormData(); | |
fd.append(name, blobFromDataURI(dataURI)); | |
return fd; | |
} | |
return { | |
upload: function (url, dataURI) { | |
return $http.post(url, formData(dataURI), { | |
// This will define the Content-Type to 'multipart/form-data' | |
// with an appropriate boundary | |
transformRequest: angular.identity, | |
headers: { | |
'Content-Type': undefined | |
} | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: