JavaScript how can I add all the file sizes together? - javascript

When I select the files I want, I want to alert all the sizes together. So if I upload 3 files and on is 100bytes and one is 200 bytes and one is 500 bytes. I want it to alert 800 bytes. At the moment its alerting each one at a time.
This is the function
function upload(e){
e.preventDefault();
e.stopPropagation();
var file = document.getElementById("file");
for(var i = 0; i < file.files.length; i++){
var each = file.files[i];
var sizes = each.size;
alert(size);
}
startupload();
}

You were using alert inside the loop :
function upload(e){
e.preventDefault();
e.stopPropagation();
var file = document.getElementById("file"),
size = 0;
for(var i = 0; i < file.files.length; i++){
var each = file.files[i];
var sizes = each.size;
size += sizes;
}
alert(size);
startupload();
}

Related

Hide/Show div p5.js

I am using the p5.js library, and I am working on a speech recognition - text to speech project. Kind of a chatbot.
Input is voice input which becomes a string.
I am outputting the result from a txt file, using a markov chain. Output is a string contained in a div.
My question is:
Is there a way to hide/show the div containing my input/output (.myMessage and .robotMessage) in intervals?
I want the whole screen first showing only the input when I am talking, then input disappears and only output showing, then when the computer voice finishes speaking my input is shown in the screen and so on...
Here some parts of the code, let me know if it is clear enough.
//bot
function setup() {
noCanvas();
//reads and checks into the text file
for (var j = 0; j < names.length; j++) {
var txt = names[j];
for (var i = 0; i <= txt.length - order; i++) {
var gram = txt.substring(i, i + order);
if (i == 0) {
beginnings.push(gram);
}
if (!ngrams[gram]) {
ngrams[gram] = [];
}
ngrams[gram].push(txt.charAt(i + order));
}
}
//voice recognition
let lang = 'en-US';
let speechRec = new p5.SpeechRec(lang, gotSpeech);
let continuous = true;
let interim = false;
speechRec.start(continuous, interim);
//text-to-speach
speech = new p5.Speech();
speech.onLoad = voiceReady;
function voiceReady() {
console.log('voice ready');
}
//input-ouput
function gotSpeech() {
if (speechRec.resultValue) {
var p = createP(speechRec.resultString);
p.class('myMessage');
}
markovIt();
chooseVoice();
speech.speak(answer);
}
}
and
function markovIt() {
var currentGram = random(beginnings);
var result = currentGram;
for (var i = 0; i < 100; i++) {
var possibilities = ngrams[currentGram];
if (!possibilities) {
break;
}
var next = random(possibilities);
result += next;
var len = result.length;
currentGram = result.substring(len - order, len);
}
var answer = result;
window.answer = answer;
var p2 = createP(answer);
p2.class('robotMessage');
}
how the HTML looks
<div class="container">
<div class="myMessage"></div>
<div class="robotMessage"></div>
</div>
Use select() to get a document element by its id, class, or tag name. e.g:
let my_div = select("myMessage");
Change the style of an element by style().
e.g hide:
my_div.style("display", "none");
e.g. show:
my_div.style("display", "block");
See also Toggle Hide and Show

How to get random images from an array and show name of image in alert without repeat of same image using JQUERY

I am developing one project in that i have requirement of get images randomly so i want alert of 9 images every time alert should show random image and it should not repeat but in my case i am getting alert of Repetative image and instead of 4 times alert I am getting 8 alerts, here i want count_time=4 which i have declared in variable below my jquery code to get random image in each alert without repeat.
<script>
//alert shoud get only 4 times because count_time is 4
$("body").on('click','.cell',function(){
var images_var = ["1.jpg", "2.jpg","3.jpg","4.jpg"];
var count_time=4;
var test = [];
var useNumbers = {};
for (i = 1; test.length < count_time; i++) {
var rng = Math.floor((Math.random() * count_time) + 1);
var random = images_var[Math.floor(Math.random()*images_var.length)];
alert(random);
if (!useNumbers[rng]) {
test.push(rng);
useNumbers[rng] = true;
}
}
});
</script>
Finally solved my issue after observing carefully and applying conidtions
<script>
$("body").on('click','.cell',function(){
var images_var = ["1.jpg", "2.jpg","3.jpg","4.jpg"];
var count_time=4;
var test = [];
var img_push=[];
var useNumbers = {};var random_img={};
for (i =1; test.length < count_time; i++) {
var rng = Math.floor((Math.random() * count_time));
var random = images_var[Math.floor(Math.random()*images_var.length)];
if ((!useNumbers[rng]) && (!random_img[random])) {
test.push(rng);
img_push.push(random);
useNumbers[rng] = true;
random_img[random] = true;
alert(random);
}
}
});
</script>

Change selected files in input element with JQuery

I need to change selected files in input:file element with Jquery. Firstly, user select files. There is a file size control in input:file change event. So, if control return false selected file should be removed in file_list. I searched this and try something for 3 - 4 hours. But not achieved still. I hope someone can help me.
Here is my function.
function handleFiles2() {
var names = [];
var newList = [];
var text = '';
var x = 0;
var fsize = 0;
var files = $(".fileUpBasvuru").get(0).files;
for (var i = 0; i < files.length; ++i) {
fsize = parseInt(files[i].size);
if (fsize > 102400) {
newList.push(files.item(i));
names.push(files[i].name);
}
}
$(".fileUpBasvuru").get(0).files = newList;
$(".file_list").html(text);
};

Photoshop Scripting: How to make page numbers

Hello I was wondering if anyone can help me write a Photoshop script that will display a number and save the document incrementally?
I got to here so far:
if(app.documents.length != 0){
var doc = app.activeDocument;
for(i = 0; i < doc.artLayers.length; ++i){
var layer = doc.artLayers[i];
if(layer.kind == LayerKind.TEXT){
layer.textItem.contents = 1;
layer.textItem.size = 20;
}
}
}
But I'm having trouble writing the content with increments. I want it so that the page number on my document increases by 1.
It isn't entirely clear what you are after. This will increment the number shown, but every text layer it encounters will get numbered.
if(app.documents.length != 0){
var doc = app.activeDocument;
var j = 1;
for(i = 0; i < doc.artLayers.length; ++i){
var layer = doc.artLayers[i];
if(layer.kind == LayerKind.TEXT){
layer.textItem.contents = j;
layer.textItem.size = 20;
j++;
}
}
}

Sum file upload input tags size

I have more than one input type=file tags in my form. Sometimes one will be hidden and sometimes there will be multiples created dynamically. I would like that on my form submit I check the file size of each file and I would sum them and check if it exceeds the maximum I set.
I wrote the code below:
function AcceptableFileUpload() {
var totalsize = 0;
var fileUploads = $("#form1 input[type=file]").length;
for (var i = 0; i < fileUploads; i++) {
//I added this to check whether there is any file here
//as I was receiving undefined when my hidden input file was checked
if ($('#form1 input[type=file]').get(i).length > 0) {
var filesize = $('#form1 input[type=file]').get(i).files[0].size;
totalsize = +filesize;
}
}
if (totalsize > 1073741) {
alert("File size limit exceeded");
return false;
}
return true;
}
When I only used var filesize = $('#form1 input[type=file]').get(0).files[0].size; I got the file size correctly, but it didn't work the way I wrote above, it says that the size is 0 always. I'd appreciate your help, thanks!
You can try
function AcceptableFileUpload() {
var totalsize = 0;
$('#form1 input:file').each(function(){
if($(this).val().length > 0){
totalsize=totalsize+$(this)[0].files[0].size;
}
});
if (totalsize > 1073741) {
alert("File size limit exceeded");
return false;
}
return true;
}

Categories

Resources