summaryrefslogtreecommitdiff
path: root/httemplate/elements/file-upload.html
blob: 034eaec3833821fc6c5ddf70a7e06f250aff39a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<SCRIPT TYPE="text/javascript">

  function doUpload(form, callback) {
    var name = 'form' + Math.floor(Math.random() * 99999); // perlize?
    var d = document.createElement('DIV');
    d.innerHTML = '<iframe style="display:none" src="about:blank" ' +
                           'id="'   + name + '" ' +
                           'name="' + name + '" ' +
                           'onload="uploadComplete(\'' + name + '\')">' +
                  '</iframe>';
    document.body.appendChild(d);

    var i = document.getElementById(name);
    if (callback && typeof(callback) == 'function') {
      i.onComplete = callback;
    }

    form.setAttribute('target', name);
    return true;
  }

  function uploadComplete(id) {
    var i = document.getElementById(id);
    if (i.contentDocument) {
      var d = i.contentDocument;
    } else if (i.contentWindow) {
      var d = i.contentWindow.document;
    } else {
      var d = window.frames[id].document;
    }
    if (d.location.href == "about:blank") {
      return;
    }

    document.getElementById('r').innerHTML = d.body.innerHTML;
    if (typeof(i.onComplete) == 'function') {
      var p;
      if (p = d.body.innerHTML.indexOf("File Upload Successful ") >= 0) {
        var v = d.body.innerHTML.substr(p+24);
        var u = document.getElementById('uploaded_files');
        v = v.substr(0, v.indexOf(';'));
        u.value = v;
        i.onComplete(true, '');
      }else{
        i.onComplete(false, d.body.innerHTML);
      }
    }
  }

</SCRIPT>

<INPUT TYPE="hidden" NAME="uploaded_files" ID="uploaded_files" VALUE="" />

<INPUT TYPE="hidden" NAME="upload_fields" VALUE="<% join(',', @field) %>" />

% foreach (@field) {
%   if($param{'no_table'}) {
  <% shift @label %> <INPUT TYPE="file" NAME="<% $_ %>" />
%   }
%   else {
    <TR>
      <TH ALIGN="<% $param{'label_align'} || 'right' %>"><% shift @label %></TH>
      <TD><INPUT TYPE="file" NAME="<% $_ %>" /></TD>
    </TR>
%   }
% }

<DIV STYLE="display:<% $param{debug} ? 'visible' : 'none' %>">
  Debugging: <PRE ID="r"></PRE>
</DIV>

<%init>

my %param = @_;

my @label = ref($param{'label'}) ? @{$param{'label'}} : ($param{'label'});
my @field = ref($param{'field'}) ? @{$param{'field'}} : ($param{'field'});

</%init>