How to do “If div class is 0 then start from 1 to append”
I'm trying to make multiple upload images via ajax with XMLHttpRequest. Everything is okay. Images post successfully and data returns as expected. So returned images are appending to related div class. But every time i upload images one by one, div class start from zero.
With this code,before to send file(s) i'm creating progressbar div, after each image loaded successfully, image replace to loder.
for (var i = 0; i < input.files.length; i++) {
var fileId = i;
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
With this function, no matter how many files, im uploading them:
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId = i
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
function uploadSingleFile(file, i) {
var fileId = i;
var ajax = new XMLHttpRequest();
//Progress Listener
ajax.upload.onprogress = function (e) {
var percent = (e.loaded / e.total) * 100;
$('#progressbar_' + fileId).animate({"width": percent + "%"},800);
};
//Load Listener
ajax.onreadystatechange = function (e) {
if (this.readyState == 4 && this.status == 200) {
var data = ajax.responseText;
var rep = JSON.parse(data);
$('#progressbar_' + fileId).css("width", "100%");
setTimeout(function(){
$('#progressbar_' + fileId).remove();
$('.uploaded_photo_grid.'+ fileId).html('');
$.each(file, function(){
if(rep.error !== ''){
$(".uploaded_photo_grid."+ fileId ).html(rep.name + ' yüklenemedi');
} else {
$(".uploaded_photo_grid."+ fileId ).html(
'<img class="previewItem" src="'+rep.fcontent+'" id="img_'+rep.id+'">');
}
});
},1500);
}
};
Rest of code...
}
Also i tried editing these lines of code. It increases the value of div for each file but just first image appears.
var zero = $('.uploaded_photo_grid.0').length;
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId;
if(zero == 0){
uploadSingleFile(this.files[i], i);
console.log(this.files[i]);
fileId = i;
} else {
uploadSingleFile(this.files[i+1], i+1);
fileId = i+1;
console.log(this.files[i]);
}
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
What should i do? Is there a way to do this better? I mean upload images without form via XML with progressbar for each file. I don't choose to use plugin, that's not for me.
php jquery xml uploader
add a comment |
I'm trying to make multiple upload images via ajax with XMLHttpRequest. Everything is okay. Images post successfully and data returns as expected. So returned images are appending to related div class. But every time i upload images one by one, div class start from zero.
With this code,before to send file(s) i'm creating progressbar div, after each image loaded successfully, image replace to loder.
for (var i = 0; i < input.files.length; i++) {
var fileId = i;
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
With this function, no matter how many files, im uploading them:
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId = i
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
function uploadSingleFile(file, i) {
var fileId = i;
var ajax = new XMLHttpRequest();
//Progress Listener
ajax.upload.onprogress = function (e) {
var percent = (e.loaded / e.total) * 100;
$('#progressbar_' + fileId).animate({"width": percent + "%"},800);
};
//Load Listener
ajax.onreadystatechange = function (e) {
if (this.readyState == 4 && this.status == 200) {
var data = ajax.responseText;
var rep = JSON.parse(data);
$('#progressbar_' + fileId).css("width", "100%");
setTimeout(function(){
$('#progressbar_' + fileId).remove();
$('.uploaded_photo_grid.'+ fileId).html('');
$.each(file, function(){
if(rep.error !== ''){
$(".uploaded_photo_grid."+ fileId ).html(rep.name + ' yüklenemedi');
} else {
$(".uploaded_photo_grid."+ fileId ).html(
'<img class="previewItem" src="'+rep.fcontent+'" id="img_'+rep.id+'">');
}
});
},1500);
}
};
Rest of code...
}
Also i tried editing these lines of code. It increases the value of div for each file but just first image appears.
var zero = $('.uploaded_photo_grid.0').length;
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId;
if(zero == 0){
uploadSingleFile(this.files[i], i);
console.log(this.files[i]);
fileId = i;
} else {
uploadSingleFile(this.files[i+1], i+1);
fileId = i+1;
console.log(this.files[i]);
}
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
What should i do? Is there a way to do this better? I mean upload images without form via XML with progressbar for each file. I don't choose to use plugin, that's not for me.
php jquery xml uploader
add a comment |
I'm trying to make multiple upload images via ajax with XMLHttpRequest. Everything is okay. Images post successfully and data returns as expected. So returned images are appending to related div class. But every time i upload images one by one, div class start from zero.
With this code,before to send file(s) i'm creating progressbar div, after each image loaded successfully, image replace to loder.
for (var i = 0; i < input.files.length; i++) {
var fileId = i;
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
With this function, no matter how many files, im uploading them:
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId = i
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
function uploadSingleFile(file, i) {
var fileId = i;
var ajax = new XMLHttpRequest();
//Progress Listener
ajax.upload.onprogress = function (e) {
var percent = (e.loaded / e.total) * 100;
$('#progressbar_' + fileId).animate({"width": percent + "%"},800);
};
//Load Listener
ajax.onreadystatechange = function (e) {
if (this.readyState == 4 && this.status == 200) {
var data = ajax.responseText;
var rep = JSON.parse(data);
$('#progressbar_' + fileId).css("width", "100%");
setTimeout(function(){
$('#progressbar_' + fileId).remove();
$('.uploaded_photo_grid.'+ fileId).html('');
$.each(file, function(){
if(rep.error !== ''){
$(".uploaded_photo_grid."+ fileId ).html(rep.name + ' yüklenemedi');
} else {
$(".uploaded_photo_grid."+ fileId ).html(
'<img class="previewItem" src="'+rep.fcontent+'" id="img_'+rep.id+'">');
}
});
},1500);
}
};
Rest of code...
}
Also i tried editing these lines of code. It increases the value of div for each file but just first image appears.
var zero = $('.uploaded_photo_grid.0').length;
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId;
if(zero == 0){
uploadSingleFile(this.files[i], i);
console.log(this.files[i]);
fileId = i;
} else {
uploadSingleFile(this.files[i+1], i+1);
fileId = i+1;
console.log(this.files[i]);
}
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
What should i do? Is there a way to do this better? I mean upload images without form via XML with progressbar for each file. I don't choose to use plugin, that's not for me.
php jquery xml uploader
I'm trying to make multiple upload images via ajax with XMLHttpRequest. Everything is okay. Images post successfully and data returns as expected. So returned images are appending to related div class. But every time i upload images one by one, div class start from zero.
With this code,before to send file(s) i'm creating progressbar div, after each image loaded successfully, image replace to loder.
for (var i = 0; i < input.files.length; i++) {
var fileId = i;
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
With this function, no matter how many files, im uploading them:
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId = i
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
function uploadSingleFile(file, i) {
var fileId = i;
var ajax = new XMLHttpRequest();
//Progress Listener
ajax.upload.onprogress = function (e) {
var percent = (e.loaded / e.total) * 100;
$('#progressbar_' + fileId).animate({"width": percent + "%"},800);
};
//Load Listener
ajax.onreadystatechange = function (e) {
if (this.readyState == 4 && this.status == 200) {
var data = ajax.responseText;
var rep = JSON.parse(data);
$('#progressbar_' + fileId).css("width", "100%");
setTimeout(function(){
$('#progressbar_' + fileId).remove();
$('.uploaded_photo_grid.'+ fileId).html('');
$.each(file, function(){
if(rep.error !== ''){
$(".uploaded_photo_grid."+ fileId ).html(rep.name + ' yüklenemedi');
} else {
$(".uploaded_photo_grid."+ fileId ).html(
'<img class="previewItem" src="'+rep.fcontent+'" id="img_'+rep.id+'">');
}
});
},1500);
}
};
Rest of code...
}
Also i tried editing these lines of code. It increases the value of div for each file but just first image appears.
var zero = $('.uploaded_photo_grid.0').length;
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId;
if(zero == 0){
uploadSingleFile(this.files[i], i);
console.log(this.files[i]);
fileId = i;
} else {
uploadSingleFile(this.files[i+1], i+1);
fileId = i+1;
console.log(this.files[i]);
}
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
What should i do? Is there a way to do this better? I mean upload images without form via XML with progressbar for each file. I don't choose to use plugin, that's not for me.
php jquery xml uploader
php jquery xml uploader
asked Nov 23 at 0:30
Plunch
74
74
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The following is the problematic line.
uploadSingleFile(this.files[i+1], i+1);
You want the value for i
passed in uploadSingleFile
to be the total number of images uploaded incremented by one.
One approach to do this is to sum $('.up_preview').children().length
, i
, and 1
. 1
is added in the sum because the in the loop i
starts at 0
.
// var zero = $('.uploaded_photo_grid.0').length;
var numImages = $('.up_preview').children().length;
if (numImages === 0) {
//...
} else {
uploadSingleFile(this.files[i+1], numImages + i + 1);
}
It works! Thank you so much
– Plunch
Nov 23 at 1:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439400%2fhow-to-do-if-div-class-is-0-then-start-from-1-to-append%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following is the problematic line.
uploadSingleFile(this.files[i+1], i+1);
You want the value for i
passed in uploadSingleFile
to be the total number of images uploaded incremented by one.
One approach to do this is to sum $('.up_preview').children().length
, i
, and 1
. 1
is added in the sum because the in the loop i
starts at 0
.
// var zero = $('.uploaded_photo_grid.0').length;
var numImages = $('.up_preview').children().length;
if (numImages === 0) {
//...
} else {
uploadSingleFile(this.files[i+1], numImages + i + 1);
}
It works! Thank you so much
– Plunch
Nov 23 at 1:58
add a comment |
The following is the problematic line.
uploadSingleFile(this.files[i+1], i+1);
You want the value for i
passed in uploadSingleFile
to be the total number of images uploaded incremented by one.
One approach to do this is to sum $('.up_preview').children().length
, i
, and 1
. 1
is added in the sum because the in the loop i
starts at 0
.
// var zero = $('.uploaded_photo_grid.0').length;
var numImages = $('.up_preview').children().length;
if (numImages === 0) {
//...
} else {
uploadSingleFile(this.files[i+1], numImages + i + 1);
}
It works! Thank you so much
– Plunch
Nov 23 at 1:58
add a comment |
The following is the problematic line.
uploadSingleFile(this.files[i+1], i+1);
You want the value for i
passed in uploadSingleFile
to be the total number of images uploaded incremented by one.
One approach to do this is to sum $('.up_preview').children().length
, i
, and 1
. 1
is added in the sum because the in the loop i
starts at 0
.
// var zero = $('.uploaded_photo_grid.0').length;
var numImages = $('.up_preview').children().length;
if (numImages === 0) {
//...
} else {
uploadSingleFile(this.files[i+1], numImages + i + 1);
}
The following is the problematic line.
uploadSingleFile(this.files[i+1], i+1);
You want the value for i
passed in uploadSingleFile
to be the total number of images uploaded incremented by one.
One approach to do this is to sum $('.up_preview').children().length
, i
, and 1
. 1
is added in the sum because the in the loop i
starts at 0
.
// var zero = $('.uploaded_photo_grid.0').length;
var numImages = $('.up_preview').children().length;
if (numImages === 0) {
//...
} else {
uploadSingleFile(this.files[i+1], numImages + i + 1);
}
answered Nov 23 at 1:04
Oluwafemi Sule
10.9k1431
10.9k1431
It works! Thank you so much
– Plunch
Nov 23 at 1:58
add a comment |
It works! Thank you so much
– Plunch
Nov 23 at 1:58
It works! Thank you so much
– Plunch
Nov 23 at 1:58
It works! Thank you so much
– Plunch
Nov 23 at 1:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439400%2fhow-to-do-if-div-class-is-0-then-start-from-1-to-append%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown