jQuery Split function not working - javascript

I am trying to split the input file object, But not working
Script
$('#imageUpload').change(function(){
readImgUrlAndPreview(this);
function readImgUrlAndPreview(input){
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var src_img = [];
src_img=$('#imageUpload')[0];
var comImgObj=dataURItoBlob(src_img);
$('#imagePreview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
else {
var img = input.value;
$('#imagePreview').attr('src',img);
}
}
});
function dataURItoBlob(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
}
Jsfiddle
I am getting this error
Uncaught TypeError: Object # has no method 'split'

Related

javascript how can i run 2 file reader methods(readAsArrayBuffer() & readAsDataURL())?

i currently have the ff code my goal is to remove the extra filreader loop? is it possible to maybe chain the readAsArrayBuffer and readAsDataURL or load them both?
var images = [];
var files = dt.files.length;
for (i = 0; i < files; i++) {
var reader = new FileReader();
reader.fileId = i;
reader.onload = function (){
images[this.fileId].is_valid = true / false; // checks mime type
}
reader.readAsArrayBuffer(dt.files[i]);
for (i = 0; i < files; i++) {
var reader = new FileReader();
reader.fileId = i;
reader.onload = function (){
//load image
if(image[i].is_valid){
// do this
}else {
// do this
}
}
reader.readAsDataURL(dt.files[i]);

Why is my function only applying SRC property only to the first uploaded image?

The function successfully creates N image elements with a class of new-avatar-picture, however, it only adds SRC property to the first image. I'm not getting any errors in the console either.
function displayInputImage(input) {
var files = input.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var reader = new FileReader();
var x = document.createElement("img");
reader.onload = function(e) {
x.setAttribute("src", e.target.result);
}
reader.readAsDataURL(file);
x.className = "new-avatar-picture";
$('.upload-btn-wrapper').append(x);
}
}
The issue with your logic is due to the fact that onload() of the reader fires after the loop completes, so x will refer to the last element in the set. Hence that single element gets its src set N times.
To fix this you could use a closure:
function displayInputImage(input) {
for (var i = 0; i < input.files.length; i++) {
var $img = $("<img />");
(function($imgElement) {
var reader = new FileReader();
reader.onload = function(e) {
$imgElement.prop("src", e.target.result);
}
reader.readAsDataURL(input.files[i]);
$imgElement.addClass("new-avatar-picture");
$('.upload-btn-wrapper').append($imgElement);
}($img));
}
}
Alternatively you could create the new img elements only after the content of the file is read:
function displayInputImage(input) {
for (var i = 0; i < input.files.length; i++) {
var reader = new FileReader();
reader.onload = function(e) {
$('<img />').addClass('new-avatar-picture').prop('src', e.target.result).appendTo('.upload-btn-wrapper');
}
reader.readAsDataURL(input.files[i]);
}
}
One way to do that is to give each image a new property, I call it temp_src so that the browser will not try to load the images right away.
Then in the .onload event, loop through all images that you have created and give each of them the proper src value, by copying it from its temp_src property.
Something like:
var reader = new FileReader();
function displayInputImage(input) {
var files = input.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var x = document.createElement("img");
x.setAttribute("class", "temp_img");
x.setAttribute("temp_src", file);
reader.readAsDataURL(file);
x.className = "new-avatar-picture";
$('.upload-btn-wrapper').append(x);
}
}
reader.onload = function(e) {
var images = document.getElementsByClassName("tmp_img");
images.forEach(function(img) {
img.setAttribute("src", img.temp_src);
});
}

I have used Javascript reader.onload = function(e) in apex code and it's not working

I have tried to pass the uploaded files value to apex method but facing some issues in javascript logic.
I have added alert after reader.onload = function(e) but didn't get any alert when I'm hitting this javascript function.
HTML CODE
function SponsorshipLetter() {
var files = document.getElementById('fileUpload');
var appId = getCookie('apex__app');
var fileName = 'Passport';
var reader = new FileReader();
reader.file = files[0];
reader.onload = function(e) {
alert('Hello 1' + document.getElementById('fileUpload').value);
var att = new sforce.SObject("Attachment");
att = fileName;
att.ContentType = this.file.type;
var binary = "";
var bytes = new Uint8Array(e.target.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
att.Body = (new sforce.Base64Binary(binary)).toString();
alert('attt');
PAP_Finances.sponsorFileUpload(att.Name, att.Body, att.ContentType, appId,
function(result, event) {
return 'Success';
});
}
reader.readAsDataURL(e.target.files[0]);
}

Reading PNG Files from Javascript - the argument is not a blob

Trying to read a .png-File with Javascript FileReader
directoryReader.readEntries(function(entries) {
for (var i=0; i<entries.length; i++) {
var reader = new FileReader();
try {
reader.readAsDataURL(entries[i]);
reader.onload = doOnload(entries[i].name);
}
catch (e) {
alert (e.message);
}
}
});
What I get is
Failed to execute 'readAsDataURL' on 'FileReader': The argument is not a Blob.
What can I do?
Found it myself:
directoryReader.readEntries(function(entries) {
for (var i=0; i<entries.length; i++) {
var reader = new FileReader();
var entry = entries[i];
entry.file(function(file){
reader.readAsDataURL(file);
reader.onload = doOnload(entry.name);
});
}
});

How to make a list of image preview

I have the following code for image preview
$("#weeklyOfferImages").change(function(){
readURLWeekly(this);
});
function readURLWeekly(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.weeklyPreview').append('<img data-src="holder.js/140x140" src="'+e.target.result'"class="offer-image img-polaroid"/>');
},
reader.readAsDataURL(input.files[0]);
}
}
this code only display one image at a time.
How can i make it display multiple images. for example the files object contains 4 files.
you only display "0" -> img input.files[0]
dynamic with length - iterating the whole array
function readURLWeekly(input) {
if(input.files)
for(var i = 0; i < input.files.length; i++) {
if (input.files[i]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.weeklyPreview').append('<img data-src="holder.js/140x140" src="' + e.target.result + '" class="offer-image img-polaroid"/>');
},
reader.readAsDataURL(input.files[i]);
}
}
}
Maybe something like this :
$("#weeklyOfferImages").change(function(){
readURLWeekly(this);
});
function readURLWeekly(input) {
if (input.files) {
for (var i=0;i<4;i++) {
if (input.files[i]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.weeklyPreview').append('<img data-src="holder.js/140x140" src="'+e.target.result'"class="offer-image img-polaroid"/>');
},
reader.readAsDataURL(input.files[i]);
} else {
break;
}
}
}
}

Categories