I have an image element where I get the blob string URL from and I'm trying to convert it to a blob first then to base64 string. so that I can send the base64 string (this is stored in #originalImage) to server side.
JS
onFinished: function (event, currentIndex) {
var form = $(this);
if ($('#image').attr('src').length) {
var selectedFile = $('#image').attr('src');
var blob;
var reader = new window.FileReader();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
blob = this.response;
console.log(this.response, typeof this.response);
if (blob != undefined) {
reader.readAsDataURL(blob);
}
}
}
xhr.open('GET', selectedFile);
xhr.responseType = 'blob';
xhr.send();
}
reader.onloadend = function () {
base64data = reader.result;
console.log(base64data);
if (base64data != undefined) {
$("#originalImage").val(base64data);
form.submit();
}
}
}
Controller
[HttpPost]
public ActionResult Action(Model blah, string croppedImage, string originalImage){
// Code here...
}
It works as expected but my only concern is that where I submit the form which is inside reader.onloadend. is there any problem with this approach or is there any better approach than this?
I appreciate any help on this, Thanks!