views:

246

answers:

1

Hi, in my current project I used a AsyncFileUpload control from AJAX Control Toolkits. After I got the async file upload part working, I needed to filter the file type so users can only upload image files. I found the following code off web and it worked well:

function uploadStarted(sender, args) {  
        var filename = args.get_fileName();  
        var filext = filename.substring(filename.lastIndexOf(".") + 1);  
        if (filext == "jpg" || filext == "jpeg" || filext == "gif" || filext == "bmp")      {
            return true;
        }
        else 
        {  
            // force uploading cancel  
            args.set_cancel(true);  
            // set reason of cancel  
            args.set_errorMessage("Invalid File Format Selected");  
            return false;  
        }  
    } 

The problem is : I don't understand this javascript. What is the type of args parameter? Where are the methods such as "get_fileName()", "set_cancel()" defined? I went to the homepage of the AsyncFileUpload control but couldn't find any documentation regarding the "args".

Can someone help me out explaining this Javascript? Thanks

A: 

I think I can answer my own question

The first parameter identifies the object that fired the event, while the second provides information on the file being uploaded. In fact, it contains five useful properties accessed using the get_abc() syntax demonstrated above.

  • get_fileName() and get_path() both return the name of the file being uploaded
  • get_length() returns the size of the file in bytes once uploaded. Returns null prior to upload
  • get_contentType() returns the mime type of the file once it is uploaded. Returns null prior to upload
  • get_errorMessage() returns an error message should one occur. Returns null otherwise

For more details refer to this article:

http://p2p.wrox.com/content/blogs/danm/enter-asyncfileupload-control

sean717