Created
September 3, 2015 23:28
-
-
Save joselo/306d901228d6735c0fba to your computer and use it in GitHub Desktop.
Piece of code to upload file
This file contains 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
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