-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cfm
139 lines (132 loc) · 3.73 KB
/
index.cfm
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<cfscript>
msg = "";
if (!structIsEmpty(form))
{
path = getDirectoryFromPath(getCurrentTemplatePath()) & form.fileName;
if (!fileExists(path))
{
content = form.fileContent;
pos = Find('base64,', content);
content = removeChars(content, 1, pos + 6);
fileWrite(path, toBinary(content));
msg = "File uploaded: #form.fileName#";
}
else
msg = "File '#form.fileName# already exists, upload concelled.";
}
</cfscript>
<cfoutput>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Drag-Drop Upload Demo</title>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body {width: 800px;}
##msg {margin: 10px 0; color: red; font-weight: bold;}
##drop-target {height: 100px; border: 1px solid ##ccc;}
.drop-target-active {background-color: ##dfd;}
input, textarea, label {display: block; width: 100%}
label {margin-top: 1em;}
</style>
</head>
<body>
<h1>Drag-Drop Upload Demo</h1>
<cfif msg neq "">
<div id="msg">#htmlEditFormat(msg)#</div>
</cfif>
<form action="#cgi.script_name#" method="POST" enctype="multipart/form-data">
Drop ONE file here
<div id="drop-target"></div>
<label for="fileName">File name</label>
<input type="text" id="fileName" name="fileName">
<label for="fileContent">File content</label>
<textarea id="fileContent" name="fileContent" rows="10" cols="80"></textarea>
<button type="submit">Upload File</button>
</form>
<script>
dropTargetInit('drop-target', true, dropHandler);
function dropTargetInit(targetID)
{
var ACTIVE_CLASS = 'drop-target-active';
var timeoutID;
addEvent(targetID, 'dragenter', function(event)
{
stopEvent(event);
addRemoveClass(targetID, ACTIVE_CLASS, true);
});
addEvent(targetID, 'dragleave', function(event)
{
stopEvent(event);
addRemoveClass(targetID, ACTIVE_CLASS, false);
if (timeoutID)
clearTimeout(timeoutID);
});
addEvent(targetID, 'dragover', function(event)
{
stopEvent(event);
if (timeoutID)
clearTimeout(timeoutID);
timeoutID = setTimeout(function() // handles dragging off the window w/o dragging off the drop targetID; ff6 needs this, maybe others, or activeClass sticks
{
timeoutID = null;
addRemoveClass(targetID, ACTIVE_CLASS, false);
}, 100);
});
addEvent(targetID, 'drop', function(event)
{
stopEvent(event);
addRemoveClass(targetID, ACTIVE_CLASS, false);
dropHandler(event);
});
}
function dropHandler()
{
var files = event.dataTransfer.files;
if (!files || files.length < 1)
{
alert('Drop files here to upload them.');
return;
}
for (var i = 0; i < files.length; i++)
handleFile(files[i]);
function handleFile(file)
{
var reader = new FileReader();
reader.onloadend = function()
{
document.getElementById('fileName').value = htmlEncode(file.name);
document.getElementById('fileContent').value = htmlEncode(reader.result);
};
reader.readAsDataURL(file);
}
}
function htmlEncode(str)
{
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
function addRemoveClass(objID, className, add)
{
var obj = document.getElementById(objID);
obj.className = add ? className : '';
}
function stopEvent(event)
{
event.preventDefault();
event.stopPropagation();
}
function addEvent(objID, types, fn)
{
obj = document.getElementById(objID);
types = types.split(',');
var type, i;
for (i = 0; i < types.length; i++)
{
type = types[i];
obj.addEventListener(type, fn, false);
}
}
</script>
</body>
</html>
</cfoutput>