Click here to Skip to main content
15,892,643 members
Articles / Web Development / ASP.NET

Asynchronous File Upload

Rate me:
Please Sign up or sign in to vote.
4.96/5 (26 votes)
13 Jun 2014CPOL12 min read 127.1K   5K   103  
An Ajax control that enables a user to upload a file asynchronously with extra data
/// <reference path="jquery-1.8.2.min.js" />

(function ($) {
    $.extend({
        createDelegate: function (context, method) {
            return function () {
                method.apply(context, arguments);
            }
        },
        createHandler: function (context, method) {
            return function (e) {
                var sender = this;
                method.call(context, sender, e);
            }
        },
        addEventHandler: function (instance, eventType, handler) {
            var event = instance.events[eventType];
            if (event == null) instance.events[eventType] = [handler];
            else {
                var index = event.length;
                event[index] = handler;
            }
        },
        removeEventHandler: function (instance, eventType, handler) {
            var event = instance.events[eventType];
            if (event == null) return;

            for (var i = 0; i < event.length; i++) {
                if (event[i] == handler) {
                    event[i] = null;
                    for (var j = i; j < event.length; j++) {
                        event[j] = event[j + 1];
                    }
                    event[event.length - 1] = null;
                    event.length = event.length - 1;

                    break;
                }
            }
        },
        raiseEvent: function (handlers, sender, e) {
            if (handlers == null) return;
            for (var i = 0; i < handlers.length; i++) {
                var handler = handlers[i];
                handler(sender, e);
            }
        },
        getBox: function (el) {
            el = $(el);
            var offset = el.offset();

            var left, right, top, bottom;
            left = offset.left;
            top = offset.top;
            right = left + el.width();
            bottom = top + el.height();

            return {
                left: left,
                right: right,
                top: top,
                bottom: bottom
            };
        },
        getMouseCoords: function (e) {
            // pageX/Y is not supported in IE
            // http://www.quirksmode.org/dom/w3c_cssom.html			
            if (!e.pageX && e.clientX) {
                // In Internet Explorer 7 some properties (mouse coordinates) are treated as physical,
                // while others are logical (offset).
                var zoom = 1;
                var body = document.body;

                if (body.getBoundingClientRect) {
                    var bound = body.getBoundingClientRect();
                    zoom = (bound.right - bound.left) / body.clientWidth;
                }

                return {
                    x: e.clientX / zoom + d.body.scrollLeft + d.documentElement.scrollLeft,
                    y: e.clientY / zoom + d.body.scrollTop + d.documentElement.scrollTop
                };
            }

            return {
                x: e.pageX,
                y: e.pageY
            };

        },
        registerNamespace: function (namespace, parent) {
            if (!parent) parent = window;

            var parts = namespace.split('.');
            var child = parts[0];
            if (!parent[child]) parent[child] = {};
            if (parts.length > 1) {
                namespace = namespace.substr(namespace.indexOf('.') + 1)
                $.registerNamespace(namespace, parent[child]);
            }
        }
    });
})(window.jQuery);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) The first Ones
Jordan Jordan
-- Life is Good.
and you can make it better

Comments and Discussions