Skip to content

Instantly share code, notes, and snippets.

@bettysteger
Last active November 2, 2020 09:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bettysteger/2057e0bd0b4142d4070db0e1175ca47e to your computer and use it in GitHub Desktop.
Save bettysteger/2057e0bd0b4142d4070db0e1175ca47e to your computer and use it in GitHub Desktop.
Upload of blob via angular-file-upload https://github.com/nervgh/angular-file-upload/issues/208
/**
* Show preview of cropped image
*/
uploader.onAfterAddingFile = function(item) {
$scope.cropped = {image: ''};
var reader = new FileReader();
reader.onload = function(event) {
$scope.$apply(function(){
$scope.cropped.image = event.target.result;
});
};
reader.readAsDataURL(item._file);
};
/**
* Upload Blob (cropped image) instead of file.
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/FormData
* https://github.com/nervgh/angular-file-upload/issues/208
*/
uploader.onBeforeUploadItem = function(item) {
var blob = dataURItoBlob($scope.cropped.image);
item._file = blob;
};
/**
* Converts data uri to Blob. Necessary for uploading.
* @see
* http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata
* @param {String} dataURI
* @return {Blob}
*/
var dataURItoBlob = function(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1]);
} else {
byteString = decodeURI(dataURI.split(',')[1]);
}
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
var array = [];
for(var i = 0; i < byteString.length; i++) {
array.push(byteString.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: mimeString});
};
@gabpy
Copy link

gabpy commented Nov 1, 2020

object returned in dataURItoBlob can be uploaded to a blob field in a mysql database?

@bettysteger
Copy link
Author

object returned in dataURItoBlob can be uploaded to a blob field in a mysql database?

i am not sure, we stored the file on the server not inside the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment