Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
5 lars 1
<!DOCTYPE HTML>
2
<!--
3
/*
4
 * jQuery File Upload Plugin postMessage API
5
 * https://github.com/blueimp/jQuery-File-Upload
6
 *
7
 * Copyright 2011, Sebastian Tschan
8
 * https://blueimp.net
9
 *
10
 * Licensed under the MIT license:
11
 * http://www.opensource.org/licenses/MIT
12
 */
13
-->
14
<html lang="en">
15
 
16
    <head>
17
        <meta charset="utf-8">
18
        <title>jQuery File Upload Plugin postMessage API</title>
19
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
20
    </head>
21
 
22
    <body>
23
        <script>
24
            /*jslint unparam: true, regexp: true */
25
            /*global $, Blob, FormData, location */
26
            'use strict';
27
            var origin = /^http:\/\/example.org/,
28
                target = new RegExp('^(http(s)?:)?\\/\\/' + location.host + '\\/');
29
            $(window).on('message', function(e)
30
            {
31
                e = e.originalEvent;
32
                var s = e.data,
33
                    xhr = $.ajaxSettings.xhr(),
34
                    f;
35
                if (!origin.test(e.origin))
36
                {
37
                    throw new Error('Origin "' + e.origin + '" does not match ' + origin);
38
                }
39
                if (!target.test(e.data.url))
40
                {
41
                    throw new Error('Target "' + e.data.url + '" does not match ' + target);
42
                }
43
                $(xhr.upload).on('progress', function(ev)
44
                {
45
                    ev = ev.originalEvent;
46
                    e.source.postMessage(
47
                    {
48
                        id: s.id,
49
                        type: ev.type,
50
                        timeStamp: ev.timeStamp,
51
                        lengthComputable: ev.lengthComputable,
52
                        loaded: ev.loaded,
53
                        total: ev.total
54
                    }, e.origin);
55
                });
56
                s.xhr = function()
57
                {
58
                    return xhr;
59
                };
60
                if (!(s.data instanceof Blob))
61
                {
62
                    f = new FormData();
63
                    $.each(s.data, function(i, v)
64
                    {
65
                        f.append(v.name, v.value);
66
                    });
67
                    s.data = f;
68
                }
69
                $.ajax(s).always(function(result, statusText, jqXHR)
70
                {
71
                    if (!jqXHR.done)
72
                    {
73
                        jqXHR = result;
74
                        result = null;
75
                    }
76
                    e.source.postMessage(
77
                    {
78
                        id: s.id,
79
                        status: jqXHR.status,
80
                        statusText: statusText,
81
                        result: result,
82
                        headers: jqXHR.getAllResponseHeaders()
83
                    }, e.origin);
84
                });
85
            });
86
        </script>
87
    </body>
88
 
89
</html>