public
Last active — forked from boucher/gist:11652

File Upload

  • Download Gist
gistfile1.m
Objective-C
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
//
// FileUpload.j
// Editor
//
// Created by Francisco Tolmasky on 03/04/08.
// Copyright 2005 - 2008, 280 North, Inc. All rights reserved.
//
 
import <Foundation/CPObject.j>
import <Foundation/CPValue.j>
import <Foundation/CPException.j>
 
var UPLOAD_IFRAME_PREFIX = "UPLOAD_IFRAME_",
UPLOAD_FORM_PREFIX = "UPLOAD_FORM_",
UPLOAD_INPUT_PREFIX = "UPLOAD_INPUT_";
 
@implementation UploadButton : CPButton
{
DOMElement _DOMIFrameElement;
DOMElement _fileUploadElement;
DOMElement _uploadForm;
function _mouseMovedCallback;
function _mouseUpCallback;
id _delegate;
CPDictionary _parameters;
}
 
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if (self)
{
var hash = [self hash];
_uploadForm = document.createElement("form");
_uploadForm.method = "POST";
_uploadForm.action = "#";
 
if(document.attachEvent)
_uploadForm.encoding = "multipart/form-data";
else
_uploadForm.enctype = "multipart/form-data";
_fileUploadElement = document.createElement("input");
_fileUploadElement.type = "file";
_fileUploadElement.name = "file";
_fileUploadElement.onmousedown = function(aDOMEvent)
{
aDOMEvent = aDOMEvent || window.event;
var x = aDOMEvent.clientX,
y = aDOMEvent.clientY,
theWindow = [self window];
[CPApp sendEvent:[CPEvent mouseEventWithType:CPLeftMouseDown location:[theWindow convertBridgeToBase:CGPointMake(x, y)]
modifierFlags:0 timestamp:0 windowNumber:[theWindow windowNumber] context:nil eventNumber:-1 clickCount:1 pressure:0]];
if (document.addEventListener)
{
document.addEventListener(CPDOMEventMouseUp, _mouseUpCallback, NO);
document.addEventListener(CPDOMEventMouseMoved, _mouseMovedCallback, NO);
}
else if(document.attachEvent)
{
document.attachEvent("on" + CPDOMEventMouseUp, _mouseUpCallback);
document.attachEvent("on" + CPDOMEventMouseMoved, _mouseMovedCallback);
}
}
 
_mouseUpCallback = function(aDOMEvent)
{
if (document.removeEventListener)
{
document.removeEventListener(CPDOMEventMouseUp, _mouseUpCallback, NO);
document.removeEventListener(CPDOMEventMouseMoved, _mouseMovedCallback, NO);
}
else if(document.attachEvent)
{
document.detachEvent("on" + CPDOMEventMouseUp, _mouseUpCallback);
document.detachEvent("on" + CPDOMEventMouseMoved, _mouseMovedCallback);
}
aDOMEvent = aDOMEvent || window.event;
var x = aDOMEvent.clientX,
y = aDOMEvent.clientY,
theWindow = [self window];
[CPApp sendEvent:[CPEvent mouseEventWithType:CPLeftMouseUp location:[theWindow convertBridgeToBase:CGPointMake(x, y)]
modifierFlags:0 timestamp:0 windowNumber:[theWindow windowNumber] context:nil eventNumber:-1 clickCount:1 pressure:0]];
}
_mouseMovedCallback = function(aDOMEvent)
{
//ASSERT(mouse is down)
aDOMEvent = aDOMEvent || window.event;
var x = aDOMEvent.clientX,
y = aDOMEvent.clientY,
theWindow = [self window];
[CPApp sendEvent:[CPEvent mouseEventWithType:CPLeftMouseDragged location:[theWindow convertBridgeToBase:CGPointMake(x, y)]
modifierFlags:0 timestamp:0 windowNumber:[theWindow windowNumber] context:nil eventNumber:-1 clickCount:1 pressure:0]];
}
_uploadForm.style.position = "absolute";
_uploadForm.style.top = "0px";
_uploadForm.style.left = "0px";
_uploadForm.style.zIndex = 1000;
_fileUploadElement.style.opacity = "0";
_fileUploadElement.style.filter = "alpha(opacity=0)";
_uploadForm.style.width = "100%";
_uploadForm.style.height = "100%";
_fileUploadElement.style.fontSize = "1000px";
if (document.attachEvent)
{
_fileUploadElement.style.position = "relative";
_fileUploadElement.style.top = "-10px";
_fileUploadElement.style.left = "-10px";
_fileUploadElement.style.width = "1px";
}
else
_fileUploadElement.style.cssFloat = "right";
_fileUploadElement.onchange = function()
{
[self uploadSelectionDidChange: _fileUploadElement.value];
};
_uploadForm.appendChild(_fileUploadElement);
_DOMElement.appendChild(_uploadForm);
_parameters = [CPDictionary dictionary];
[self setBordered:NO];
}
return self;
}
 
- (void)setDelegate:(id)aDelegate
{
_delegate = aDelegate;
}
 
- (id)delegate
{
return _delegate;
}
 
- (void)setURL:(CPString)aURL
{
_uploadForm.action = aURL;
}
 
- (void)uploadSelectionDidChange:(CPString)selection
{
if ([_delegate respondsToSelector:@selector(uploadButton:didChangeSelection:)])
[_delegate uploadButton: self didChangeSelection: selection];
}
 
- (CPString)selection
{
return _fileUploadElement.value;
}
 
- (void)resetSelection
{
_fileUploadElement.value = "";
}
 
- (void)uploadDidFinishWithResponse:(CPString)response
{
if ([_delegate respondsToSelector:@selector(uploadButton:didFinishUploadWithData:)])
[_delegate uploadButton: self didFinishUploadWithData: response];
}
 
- (void)uploadDidFailWithError:(CPString)anError
{
if ([_delegate respondsToSelector:@selector(uploadButton:didFailWithError:)])
[_delegate uploadButton: self didFailWithError: anError];
}
 
- (BOOL)setValue:(CPString)aValue forParameter:(CPString)aParam
{
if(aParam == "file")
return NO;
[_parameters setObject:aValue forKey:aParam];
return YES;
}
 
- (void)parameters
{
return _parameters;
}
 
- (void)submit
{
_uploadForm.target = "FRAME_"+(new Date());
 
//remove existing parameters
var index = _uploadForm.childNodes.length;
while(index--)
_uploadForm.removeChild(_uploadForm.childNodes[index]);
//append the parameters to the form
var keys = [_parameters allKeys];
for(var i = 0, count = keys.length; i<count; i++)
{
var element = document.createElement("input");
element.type = "hidden";
element.name = keys[i];
element.value = [_parameters objectForKey:keys[i]];
_uploadForm.appendChild(element);
}
_uploadForm.appendChild(_fileUploadElement);
 
if(_DOMIFrameElement)
{
document.body.removeChild(_DOMIFrameElement);
_DOMIFrameElement.onload = nil;
_DOMIFrameElement = nil;
}
if(window.attachEvent)
{
_DOMIFrameElement = document.createElement("<iframe id=\"" + _uploadForm.target + "\" name=\"" + _uploadForm.target + "\" />");
if(window.location.href.toLowerCase().indexOf("https") === 0)
_DOMIFrameElement.src = "javascript:false";
}
else
{
_DOMIFrameElement = document.createElement("iframe");
_DOMIFrameElement.name = _uploadForm.target;
}
_DOMIFrameElement.style.width = "1px";
_DOMIFrameElement.style.height = "1px";
_DOMIFrameElement.style.zIndex = -1000;
_DOMIFrameElement.style.opacity = "0";
_DOMIFrameElement.style.filter = "alpha(opacity=0)";
document.body.appendChild(_DOMIFrameElement);
 
_onloadHandler = function()
{
try
{
var responseText = _DOMIFrameElement.contentWindow.document.body ? _DOMIFrameElement.contentWindow.document.body.innerHTML :
_DOMIFrameElement.contentWindow.document.documentElement.textContent;
 
[self uploadDidFinishWithResponse: responseText];
window.setTimeout(function(){
document.body.removeChild(_DOMIFrameElement);
_DOMIFrameElement.onload = nil;
_DOMIFrameElement = nil;
}, 100);
}
catch (e)
{
[self uploadDidFailWithError:e];
}
}
if (window.attachEvent)
{
_DOMIFrameElement.onreadystatechange = function()
{
if (this.readyState == "loaded" || this.readyState == "complete")
_onloadHandler();
}
}
 
_DOMIFrameElement.onload = _onloadHandler;
 
_uploadForm.submit();
if ([_delegate respondsToSelector:@selector(uploadButtonDidBeginUpload:)])
[_delegate uploadButtonDidBeginUpload:self];
}
 
- (void)disposeOfEvent:(CPEvent)anEvent
{
if ([anEvent type] == CPLeftMouseDown)
[CPApp setTarget:self selector:@selector(disposeOfEvent:) forNextEventMatchingMask:CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
}
 
- (void)mouseDown:(CPEvent)anEvent
{
[CPApp setTarget:self selector:@selector(disposeOfEvent:) forNextEventMatchingMask:CPLeftMouseDownMask untilDate:nil inMode:nil dequeue:YES];
 
[super mouseDown:anEvent];
}
 
@end
 
function _CPGUID()
{
var g = "";
for(var i = 0; i < 32; i++)
g += Math.floor(Math.random() * 0xF).toString(0xF);
return g;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.