Skip to content

Instantly share code, notes, and snippets.

@joselo
Created September 3, 2015 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joselo/306d901228d6735c0fba to your computer and use it in GitHub Desktop.
Save joselo/306d901228d6735c0fba to your computer and use it in GitHub Desktop.
Piece of code to upload file
function uploadFile(data, type) {
blobUtil.base64StringToBlob(data).then(function (blob) {
db.putAttachment($scope.ngDocId, $scope.attachmentId, $scope.ngModelRev, blob, type).then(function (result) {
$scope.ngModelRev = result.rev;
loadPreview();
}).catch(function (err) {
console.log(err);
});
}).catch(function (err) {
// image failed to load
});
}
$scope.upload = function (files) {
if (files && files.length) {
var file = files[0];
var reader = new FileReader();
reader.onload = function (e) {
var encodedData = reader.result.replace("data:" + file.type + ";base64,", '');
uploadFile(encodedData, file.type);
};
reader.readAsDataURL(file);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment