forked from mailru/FileAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadFileCommand.as
More file actions
133 lines (114 loc) · 4.01 KB
/
UploadFileCommand.as
File metadata and controls
133 lines (114 loc) · 4.01 KB
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
package ru.mail.commands
{
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TextEvent;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import ru.mail.data.vo.ErrorVO;
import ru.mail.utils.LoggerJS;
/**
* Upload using fileReference.upload()
* @author v.demidov
*
*/
public class UploadFileCommand extends AbstractUploadFileCommand
{
private var fileRef:FileReference;
private var status:String = null; // httpStatus. in case of upload error we get httpStatus event followed by ioError event. add status to error event using this temp variable
public function UploadFileCommand(fileRef:FileReference, url:String, headers:Object, uploadPostData:Object, uploadDataFieldName:String)
{
super(url, headers, uploadPostData, uploadDataFieldName);
this.fileRef = fileRef;
}
override public function dispose():void
{
fileRef.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCompleteData);
fileRef.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
fileRef.removeEventListener(IOErrorEvent.IO_ERROR, onError);
fileRef.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
fileRef.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
override public function execute():void
{
if ( _url == null ) {
complete(false, null, new ErrorVO("UploadFileCommand: upload url is null") );
return;
}
// add listeners
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCompleteData);
fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, onError);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress);
// create request
var request:URLRequest = new URLRequest(_url);
// data
request.method = URLRequestMethod.POST;
request.data = _uploadPostData;
LoggerJS.log("upload file with FileReference, url = " + request.url);
try
{
fileRef.upload(request, _uploadDataFieldName) ;
}
catch ( e:Error ){
trace ("UploadFileCommand execute err", e);
complete( false, null, new ErrorVO( e.toString() ) );
}
}
/**
* Cancels upload and disposes the object
*
* Known issue: do not call cancel when the progress is 100% but uploadCompleteData hasn't received
* in this case the file fill not be deleted from server
*/
override public function cancel():void
{
try{
trace ("UploadFileCommand:cancel");
fileRef.cancel();
dispose();
}catch(e:Error){
trace ("UploadFileCommand cancel() error: "+e.message);
}
}
/**
* Get the responce from server
*/
private function onUploadCompleteData(event:DataEvent):void
{
trace ("onUploadCompleteData", event);
complete(true, event.data);
}
private function onHTTPStatus(event:HTTPStatusEvent):void
{
trace ("onHTTPStatus", event);
LoggerJS.log("fileReference.upload HTTPStatusEvent: " +event.status);
status = event.status.toString();
dispatchEvent(new TextEvent("httpStatus", false, false, event.status.toString() ) );
}
private function onError(event:Event):void
{
// choose error type
var errorType:String = null;
if (event is IOErrorEvent) {
errorType = "IOError";
} else if (event is SecurityErrorEvent) {
errorType = "SecurityError";
}
LoggerJS.log("fileReference.upload onError: " +event.toString());
trace ("onError", event);
complete(false, null, new ErrorVO(event.toString(), errorType, status) );
}
private function onProgress(event:ProgressEvent):void
{
// because of bug (loaded:123123, total:0), use fileRef.size as total
dispatchEvent(new ProgressEvent( ProgressEvent.PROGRESS, false, false, event.bytesLoaded, fileRef.size) );
}
}
}