File-type (Image) validation before upload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    File-type (Image) validation before upload

    How can I add my validation script to this file upload code?

    Validation Script:[code=javascript]function validate(frm) {
    if (!/(\.(gif|jpg|jpe g|png))$/i.test(frm.file .value)){
    alert('Invalid file type.\nPlease attach a valid image file');
    frm.file.focus( );
    return false;
    }
    return true;
    }[/code]
    Upload Script[code=javascript]function startCallback() {
    //VALIDATION SCRIPT NEEDS TO BE ADDED HERE
    }
    function completeCallbac k(response) {
    dcdRes(response );
    }
    AIM = {

    frame : function(c) {

    var n = 'f' + Math.floor(Math .random() * 99999);
    var d = document.create Element('DIV');
    d.innerHTML = '<iframe style="display: none;" src="about:blan k" id="'+n+'" name="'+n+'" onload="AIM.loa ded(\''+n+'\')" ></iframe>';
    document.body.a ppendChild(d);

    var i = document.getEle mentById(n);
    if (c && typeof(c.onComp lete) == 'function') {
    i.onComplete = c.onComplete;
    }

    return n;
    },

    form : function(f, name) {
    f.setAttribute( 'target', name);
    },

    submit : function(f, c) {
    AIM.form(f, AIM.frame(c));
    if (c && typeof(c.onStar t) == 'function') {
    return c.onStart();
    } else {
    return true;
    }
    },

    loaded : function(id) {
    var i = document.getEle mentById(id);
    if (i.contentDocum ent) {
    var d = i.contentDocume nt;
    } else if (i.contentWindo w) {
    var d = i.contentWindow .document;
    } else {
    var d = window.frames[id].document;
    }
    if (d.location.hre f == "about:blan k") {
    return;
    }

    if (typeof(i.onCom plete) == 'function') {
    i.onComplete(d. body.innerHTML) ;
    }
    }
    }//got the code from www.webtoolkit. info[/code]
    HTML File[html]<form action="upload. php" method="post" onsubmit="retur n AIM.submit(this , {'onStart' : startCallback, 'onComplete' : completeCallbac k})" enctype="multip art/form-data">
    <input type="file" name="file" />
    <input type="submit" name="submit" value="Upload" />
    </form>[/html]

    Its working too good, but I need the validation first.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Pass 'f' to c.onStart().

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by acoder
      Pass 'f' to c.onStart().
      I had done that and it's working fine.

      But now the problem is, how can is pass the same argument, f to completeCallbac k function?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Do you mean startCallback?

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by acoder
          Do you mean startCallback?
          No, that's done.

          I'm talking about line 4 in the upload script.

          Means after uploading is done, I want to know which form has uploaded this image, so I can add image element in the corresponding row. There are multiple uploads.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Add a second parameter to the callback function and pass that in frame and loaded too so that it can be passed to completeCallbac k().

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by acoder
              Add a second parameter to the callback function and pass that in frame and loaded too so that it can be passed to completeCallbac k().
              I passed the parameter in form as this completeCallbac k('img_sfsdag')
              * img_sfsdag is the id of the row.
              [CODE=javascript]function completeCallbac k(id, response) {
              alert(id); //shows correct value, ie. img_sfsdag
              alert(response) ; //shows undefined
              }[/code]But after that, instead of staying on same page, the browser is moving to upload.php, the form action.

              So, now completeCallbac k() not reading the response properly.

              Even earlier there wasn't any response argument passed, and it worked fine.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                I meant soething like this:
                [code=javascript]function completeCallbac k(f, response) {
                // make use of form here.
                dcdRes(response );
                }
                AIM = {

                frame : function(f, c) {

                var n = 'f' + Math.floor(Math .random() * 99999);
                var d = document.create Element('DIV');
                d.innerHTML = '<iframe style="display: none;" src="about:blan k" id="'+n+'" name="'+n+'" onload="AIM.loa ded(\''+f+','+n +'\')"></iframe>';
                document.body.a ppendChild(d);

                var i = document.getEle mentById(n);
                if (c && typeof(c.onComp lete) == 'function') {
                i.onComplete = c.onComplete;
                }

                return n;
                },

                form : function(f, name) {
                f.setAttribute( 'target', name);
                },

                submit : function(f, c) {
                AIM.form(f, AIM.frame(f, c));
                if (c && typeof(c.onStar t) == 'function') {
                return c.onStart();
                } else {
                return true;
                }
                },

                loaded : function(f, id) {
                var i = document.getEle mentById(id);
                if (i.contentDocum ent) {
                var d = i.contentDocume nt;
                } else if (i.contentWindo w) {
                var d = i.contentWindow .document;
                } else {
                var d = window.frames[id].document;
                }
                if (d.location.hre f == "about:blan k") {
                return;
                }

                if (typeof(i.onCom plete) == 'function') {
                i.onComplete(f, d.body.innerHTM L);
                }
                }
                }[/code]The response is d.body.innerHTM L.

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Thank you too much!!

                  Now I understand how was the response reaching the completeCallbac k() function. That was my main confusion! I was stuck here for 2 days.

                  Now its working fine!

                  Thanks,

                  Harpreet

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You're welcome! Glad you have it working.

                    Comment

                    Working...