Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1

    Javascript: Dimensioni e peso files

    Buongiorno a tutti,

    sto preparando un form per permettere l'upload di una immagine oltre ad altre informazioni.

    Avrei bisogno di conoscere il peso del file che l'utente vuole caricare per limitare la dimensione dei files che giungono sul server.

    Inoltre avrei bisogno di porre alcuni limiti alle immagini che un utente vuole caricare: es: widthMAX = 200px heightMAX = 120px per evitare che siano strecchate troppo.

    Come posso ovviare al problema?

    Se possibile preferisco effettuare il controllo lato client, con un javascrpt per evitare di mandare la richiesta sul server e poi farla tornare indietro con l'errore.

    Qualche anima pia può darmi una mano?

    Grazie mille e buona giornata
    ...God must love stupid people...He made so many...

    www.eurodomus.org

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    qualcosa lato client e' possibile farla solo con IE,
    interessa?

  3. #3
    Ciao, certo che mi interessa.
    Farlo lato client è meglio così non faccio eseguire i controlli sul server. Grazie mille e buona giornata.
    ...God must love stupid people...He made so many...

    www.eurodomus.org

  4. #4
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    qui con activex da autorizzare
    http://forum.html.it/forum/showthrea...hreadid=637900

    un limite al peso per il solo ie consiste nell' inserire
    <input type="hidden" name="MAX_FILE_SIZE" value="pesoespressoinbytes" />
    prima del campo con input type="file"

    sinceramente non ho mai approfondito ne' provato
    prova con una ricerca su google con chiave max_file_size

    ciao

  5. #5
    Grazie mille, sei stato molto gentile.

    Ho trovato comunque un altro javascript che sembra fungere.

    Se ti può interessare lo "posto", però stasera appena arrivo a casa.

    Vito
    ...God must love stupid people...He made so many...

    www.eurodomus.org

  6. #6
    Funge.

    Ciao,

    Vito


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Test</title>
    <script language="JavaScript">
    <!--
    function checkFileUpload(form,extensions,requireUpload,size Limit,minWidth,minHeight,maxWidth,maxHeight,saveWi dth,saveHeight) { //v2.08
    document.MM_returnValue = true;
    for (var i = 0; i<form.elements.length; i++) {
    field = form.elements[i];
    if (field.type.toUpperCase() != 'FILE') continue;
    checkOneFileUpload(field,extensions,requireUpload, sizeLimit,minWidth,minHeight,maxWidth,maxHeight,sa veWidth,saveHeight);
    } }

    function checkOneFileUpload(field,extensions,requireUpload, sizeLimit,minWidth,minHeight,maxWidth,maxHeight,sa veWidth,saveHeight) { //v2.08
    document.MM_returnValue = true;
    if (extensions != '') var re = new RegExp("\.(" + extensions.replace(/,/gi,"|").replace(/s/gi,"") + ")$","i");
    if (field.value == '') {
    if (requireUpload) {alert('File is required!');document.MM_returnValue = false;field.focus();return;}
    } else {
    if(extensions != '' && !re.test(field.value)) {
    alert('This file type is not allowed for uploading.\nOnly the following file extensions are allowed: ' + extensions + '.\nPlease select another file and try again.');
    document.MM_returnValue = false;field.focus();return;
    }
    document.PU_uploadForm = field.form;
    re = new RegExp(".(gif|jpg|png|bmp|jpeg)$","i");
    if(re.test(field.value) && (sizeLimit != '' || minWidth != '' || minHeight != '' || maxWidth != '' || maxHeight != '' || saveWidth != '' || saveHeight != '')) {
    checkImageDimensions(field,sizeLimit,minWidth,minH eight,maxWidth,maxHeight,saveWidth,saveHeight);
    } }
    }

    function showImageDimensions(fieldImg) { //v2.08
    var isNS6 = (!document.all && document.getElementById ? true : false);
    var img = (fieldImg && !isNS6 ? fieldImg : this);
    if ((img.minWidth != '' && img.minWidth > img.width) || (img.minHeight != '' && img.minHeight > img.height)) {
    alert('Uploaded Image is too small!\nShould be at least ' + img.minWidth + ' x ' + img.minHeight); return;}
    if ((img.maxWidth != '' && img.width > img.maxWidth) || (img.maxHeight != '' && img.height > img.maxHeight)) {
    alert('Uploaded Image is too big!\nShould be max ' + img.maxWidth + ' x ' + img.maxHeight); return;}
    if (img.sizeLimit != '' && img.fileSize > img.sizeLimit) {
    alert('Uploaded Image File Size is too big!\nShould be max ' + (img.sizeLimit/1024) + ' KBytes'); return;}
    if (img.saveWidth != '') document.PU_uploadForm[img.saveWidth].value = img.width;
    if (img.saveHeight != '') document.PU_uploadForm[img.saveHeight].value = img.height;
    document.MM_returnValue = true;
    }

    function checkImageDimensions(field,sizeL,minW,minH,maxW,ma xH,saveW,saveH) { //v2.08
    if (!document.layers) {
    var isNS6 = (!document.all && document.getElementById ? true : false);
    document.MM_returnValue = false; var imgURL = 'file:///' + field.value.replace(/\\/gi,'/');
    if (!field.gp_img || (field.gp_img && field.gp_img.src != imgURL) || isNS6) {field.gp_img = new Image();
    with (field) {gp_img.sizeLimit = sizeL*1024; gp_img.minWidth = minW; gp_img.minHeight = minH; gp_img.maxWidth = maxW; gp_img.maxHeight = maxH;
    gp_img.saveWidth = saveW; gp_img.saveHeight = saveH; gp_img.onload = showImageDimensions; gp_img.src = imgURL; }
    } else showImageDimensions(field.gp_img);}
    }

    function MM_findObj(n, d) { //v4.01
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_valida teForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
    } else if (test!='R') {
    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
    }
    //-->
    </script>
    </head>

    <body>
    <form action="#" method="POST" name="form1" enctype="multipart/form-data" onSubmit="checkFileUpload(this,'GIF,JPG,PNG',false ,100,220,160,'440','320','220','160');return document.MM_returnValue;MM_validateForm('metri','' ,'RisNum','via','','R','citta','','R','descrizione ','','R');return document.MM_returnValue">
    <table cellpadding="1" cellspacing="1">
    <tr>
    <td>Immagine:</td>
    <td><input type="file" name="file" onChange="checkOneFileUpload(this,'GIF,JPG,PNG',fa lse,100,220,160,'440','320','220','160')"></td>
    </tr>
    <tr valign="baseline">
    <td><input type="submit" value="Invia" name="submit"></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    ...God must love stupid people...He made so many...

    www.eurodomus.org

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    310
    Originariamente inviato da gadavitor
    Funge.

    Ciao,

    Vito


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Test</title>
    <script language="JavaScript">
    <!--
    function checkFileUpload(form,extensions,requireUpload,size Limit,minWidth,minHeight,maxWidth,maxHeight,saveWi dth,saveHeight) { //v2.08
    document.MM_returnValue = true;
    for (var i = 0; i<form.elements.length; i++) {
    field = form.elements[i];
    if (field.type.toUpperCase() != 'FILE') continue;
    checkOneFileUpload(field,extensions,requireUpload, sizeLimit,minWidth,minHeight,maxWidth,maxHeight,sa veWidth,saveHeight);
    } }

    function checkOneFileUpload(field,extensions,requireUpload, sizeLimit,minWidth,minHeight,maxWidth,maxHeight,sa veWidth,saveHeight) { //v2.08
    document.MM_returnValue = true;
    if (extensions != '') var re = new RegExp("\.(" + extensions.replace(/,/gi,"|").replace(/s/gi,"") + ")$","i");
    if (field.value == '') {
    if (requireUpload) {alert('File is required!');document.MM_returnValue = false;field.focus();return;}
    } else {
    if(extensions != '' && !re.test(field.value)) {
    alert('This file type is not allowed for uploading.\nOnly the following file extensions are allowed: ' + extensions + '.\nPlease select another file and try again.');
    document.MM_returnValue = false;field.focus();return;
    }
    document.PU_uploadForm = field.form;
    re = new RegExp(".(gif|jpg|png|bmp|jpeg)$","i");
    if(re.test(field.value) && (sizeLimit != '' || minWidth != '' || minHeight != '' || maxWidth != '' || maxHeight != '' || saveWidth != '' || saveHeight != '')) {
    checkImageDimensions(field,sizeLimit,minWidth,minH eight,maxWidth,maxHeight,saveWidth,saveHeight);
    } }
    }

    function showImageDimensions(fieldImg) { //v2.08
    var isNS6 = (!document.all && document.getElementById ? true : false);
    var img = (fieldImg && !isNS6 ? fieldImg : this);
    if ((img.minWidth != '' && img.minWidth > img.width) || (img.minHeight != '' && img.minHeight > img.height)) {
    alert('Uploaded Image is too small!\nShould be at least ' + img.minWidth + ' x ' + img.minHeight); return;}
    if ((img.maxWidth != '' && img.width > img.maxWidth) || (img.maxHeight != '' && img.height > img.maxHeight)) {
    alert('Uploaded Image is too big!\nShould be max ' + img.maxWidth + ' x ' + img.maxHeight); return;}
    if (img.sizeLimit != '' && img.fileSize > img.sizeLimit) {
    alert('Uploaded Image File Size is too big!\nShould be max ' + (img.sizeLimit/1024) + ' KBytes'); return;}
    if (img.saveWidth != '') document.PU_uploadForm[img.saveWidth].value = img.width;
    if (img.saveHeight != '') document.PU_uploadForm[img.saveHeight].value = img.height;
    document.MM_returnValue = true;
    }

    function checkImageDimensions(field,sizeL,minW,minH,maxW,ma xH,saveW,saveH) { //v2.08
    if (!document.layers) {
    var isNS6 = (!document.all && document.getElementById ? true : false);
    document.MM_returnValue = false; var imgURL = 'file:///' + field.value.replace(/\\/gi,'/');
    if (!field.gp_img || (field.gp_img && field.gp_img.src != imgURL) || isNS6) {field.gp_img = new Image();
    with (field) {gp_img.sizeLimit = sizeL*1024; gp_img.minWidth = minW; gp_img.minHeight = minH; gp_img.maxWidth = maxW; gp_img.maxHeight = maxH;
    gp_img.saveWidth = saveW; gp_img.saveHeight = saveH; gp_img.onload = showImageDimensions; gp_img.src = imgURL; }
    } else showImageDimensions(field.gp_img);}
    }

    function MM_findObj(n, d) { //v4.01
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_valida teForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
    } else if (test!='R') {
    if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
    }
    //-->
    </script>
    </head>

    <body>
    <form action="#" method="POST" name="form1" enctype="multipart/form-data" onSubmit="checkFileUpload(this,'GIF,JPG,PNG',false ,100,220,160,'440','320','220','160');return document.MM_returnValue;MM_validateForm('metri','' ,'RisNum','via','','R','citta','','R','descrizione ','','R');return document.MM_returnValue">
    <table cellpadding="1" cellspacing="1">
    <tr>
    <td>Immagine:</td>
    <td><input type="file" name="file" onChange="checkOneFileUpload(this,'GIF,JPG,PNG',fa lse,100,220,160,'440','320','220','160')"></td>
    </tr>
    <tr valign="baseline">
    <td><input type="submit" value="Invia" name="submit"></td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    ma funziona solo con Internet Explorer?
    o lo posso usare anche con mozilla?

  8. #8
    Mi sa che non tutti i controlli funzionano con Moz, ma in questo momento sono impreparato....non mi ricordo....
    ...God must love stupid people...He made so many...

    www.eurodomus.org

  9. #9
    funziona perfettamente con Mozilla Firefox 2
    *** Rachele TI AMO DA MORIRE!!! ***

  10. #10
    Mi associo!!!

    Buona giornata a tutti.
    ...God must love stupid people...He made so many...

    www.eurodomus.org

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.