The following code is an ActionScript 3.0 code that uploads files via FileReference.
다음은 FileReference를 이용하여 파일을 업로드하는 액션스크립트3 코드입니다.
Sorry, Japanese translation is not yet available.
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
| var serverPath:String = "/";
// Creates a FileReference object.
var fr:FileReference = new FileReference();
var frFilter:FileFilter = new FileFilter("JPGs", "*.jpg");
// Adds events to the FileReference object.
fr.addEventListener(Event.SELECT, fileSelected);
fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileUploaded);
fr.addEventListener(ProgressEvent.PROGRESS, fileProgress);
fr.addEventListener(IOErrorEvent.IO_ERROR, errorOccured);
/**
* Executes when a user selects a file to upload.
*/
function fileSelected(evt:Event):void{
// Checks the size of the file.
if(fr.size > 200*1024){
trace("You can NOT upload an image lagerer than 200KB.");
}else{
var ur:URLRequest = new URLRequest();
ur.url = serverPath + "test/uploadok.php";
// Puts newName parameter, to avoid the multi-byte filename problem.
var uv:URLVariables = new URLVariables();
uv.newName = "upload.jpg";
ur.data = uv;
// Starts uploading.
fr.upload(ur);
}// end if else [fr.size]
}// end fileSelected
/**
* Executes when a file upload is finished.
*/
function fileUploaded(evt:DataEvent):void{
trace(evt.data);
}// end fileUploaded
/**
* Executes while uploading a file.
*/
function fileProgress(evt:ProgressEvent):void{
//trace(evt.bytesLoaded + " / " + evt.bytesTotal);
}// end fileProgress
/**
* Executes when an IO error occurs.
*/
function errorOccured(evt:IOErrorEvent):void{
trace("IO error has occured.");
}// end errorOccured
/**
* Shows the file browser when clicks on the stage.
*/
stage.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
function clicked(evt:MouseEvent):void{
fr.browse([frFilter]);
} |
var serverPath:String = "/";
// Creates a FileReference object.
var fr:FileReference = new FileReference();
var frFilter:FileFilter = new FileFilter("JPGs", "*.jpg");
// Adds events to the FileReference object.
fr.addEventListener(Event.SELECT, fileSelected);
fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileUploaded);
fr.addEventListener(ProgressEvent.PROGRESS, fileProgress);
fr.addEventListener(IOErrorEvent.IO_ERROR, errorOccured);
/**
* Executes when a user selects a file to upload.
*/
function fileSelected(evt:Event):void{
// Checks the size of the file.
if(fr.size > 200*1024){
trace("You can NOT upload an image lagerer than 200KB.");
}else{
var ur:URLRequest = new URLRequest();
ur.url = serverPath + "test/uploadok.php";
// Puts newName parameter, to avoid the multi-byte filename problem.
var uv:URLVariables = new URLVariables();
uv.newName = "upload.jpg";
ur.data = uv;
// Starts uploading.
fr.upload(ur);
}// end if else [fr.size]
}// end fileSelected
/**
* Executes when a file upload is finished.
*/
function fileUploaded(evt:DataEvent):void{
trace(evt.data);
}// end fileUploaded
/**
* Executes while uploading a file.
*/
function fileProgress(evt:ProgressEvent):void{
//trace(evt.bytesLoaded + " / " + evt.bytesTotal);
}// end fileProgress
/**
* Executes when an IO error occurs.
*/
function errorOccured(evt:IOErrorEvent):void{
trace("IO error has occured.");
}// end errorOccured
/**
* Shows the file browser when clicks on the stage.
*/
stage.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
function clicked(evt:MouseEvent):void{
fr.browse([frFilter]);
}
In line 27 through 30, I assign newName parameter to an URLVariables object. This is to avoid IO error which occurs if the filename contains 2-byte characters. If you let the serverscript rename the uploaded file, this is not needed.
27-30번째 줄을 보면, URLVariables 객체에 newName 파라메터를 설정했습니다. 이것은 파일명에 한글과 같은 2바이트 문자가 포함된 경우 서버에서 파일 저장시에 IO 에러가 발생하기 때문에, 이를 피하기 위해서 newName에 별도의 파일명을 저장한 것입니다. 만약 서버측에서 파일명을 변경하도록 구성되어 있다면, 이 부분은 필요 없습니다.
27
28
29
30
| var uv:URLVariables = new URLVariables();
uv.newName = "upload.jpg";
ur.data = uv; |
var uv:URLVariables = new URLVariables();
uv.newName = "upload.jpg";
ur.data = uv;