알고 보면 정말 간단한. FLEX + ABC 파일 업로드 이다.
그냥 Flex 부분과 ASP 부분을 복사해서 사용하면 된다.
ABC 업로드는 만들었지만 DEXT 업로드로 파일 업로드는 ASP 부분에서 에러가 나서 포기 ;;
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.controls.*;
import mx.events.*;
import flash.events.*;
import flash.net.*;
private function initApp():void {
Security.allowDomain("*");
}
public var _refAddFiles:FileReference;
private function fileup():void {
_refAddFiles = new FileReference();
_refAddFiles.addEventListener(Event.SELECT, selectHandler);
_refAddFiles.addEventListener(Event.COMPLETE, completeHandler);
_refAddFiles.browse();
}
private function selectHandler(event:Event):void {
//var request:URLRequest = new URLRequest("http://www.mentalhealth.or.kr/flex/Gallery/Upload.asp");
var request:URLRequest = new URLRequest("http://www.fileUpload.co.ke./upload.asp");
// new URLRequest () 안에 실지적으로 파일 업로드를 실행하는 ASP 주소을 적는다.
request.method = URLRequestMethod.POST;
_refAddFiles.upload(request,"Filedata");
}
private function completeHandler(event:Event):void {
Alert.show("File(s) have been uploaded.event", "Upload successful");
error.text = event.type;
}
]]>
</mx:Script>
<mx:Canvas top="10" bottom="10" left="10" right="10">
<mx:Panel width="300" height="266" layout="absolute" horizontalCenter="0" verticalCenter="0" id="panUpload" title="flex->ASP간 파일업로드 예제">
<mx:VBox left="10" bottom="10" top="10" right="10">
<mx:TextArea id="error" text="" width="100%" height="100%" />
</mx:VBox>
<mx:ControlBar horizontalAlign="right">
<mx:Button label="파일올리기" id="btnUpload" click="fileup();" />
</mx:ControlBar>
</mx:Panel>
</mx:Canvas>
</mx:Application>
ASP 부분이다.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="949"%>
<%
Dim abc, oFile,DirectoryPath
Set abc = Server.CreateObject("ABCUpload4.XForm")
DirectoryPath = Request.ServerVariables("APPL_PHYSICAL_PATH") & "\upload_files"
abc.AbsolutePath = True
abc.CodePage = 65001
Set oFile = abc("filedata")(1)
Dim strFileName1, FileSize1, strFileWholePath1
'실제적인 파일 업로드를 처리하는 부분
If oFile.FileExists Then
strFileName1 = oFile.SafeFileName
FileSize1 = oFile.Length
if oFile.Length > 4096000 then
Response.Write "<script language=javascript>"
Response.Write "alert(""4M 이상의 사이즈인 파일은 업로드하실 수 없습니다"");"
Response.Write "history.back();"
Response.Write "</script>"
Response.end
else
strFileWholePath1 = GetUniqueName(strFileName1, DirectoryPath)
oFile.Save strFileWholePath1
End If
Function GetUniqueName(byRef strFileName, DirectoryPath)
Dim strName, strExt
strName = Mid(strFileName, 1, InstrRev(strFileName, ".") - 1) ' 확장자를 제외한 파일명을 얻는다.
strExt = Mid(strFileName, InstrRev(strFileName, ".") + 1) '확장자를 얻는다
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim bExist : bExist = True
'우선 같은이름의 파일이 존재한다고 가정
Dim strFileWholePath : strFileWholePath = DirectoryPath & "\" & strName & "." & strExt
'저장할 파일의 완전한 이름(완전한 물리적인 경로) 구성
Dim countFileName : countFileName = 0
'파일이 존재할 경우, 이름 뒤에 붙일 숫자를 세팅함.
Do While bExist ' 우선 있다고 생각함.
If (fso.FileExists(strFileWholePath)) Then ' 같은 이름의 파일이 있을 때
countFileName = countFileName + 1 '파일명에 숫자를 붙인 새로운 파일 이름 생성
strFileName = strName & "(" & countFileName & ")." & strExt
strFileWholePath = DirectoryPath & "\" & strFileName
Else
bExist = False
End If
Loop
GetUniqueName = strFileWholePath
End Function
%>
_refAddFiles.upload(request,"Filedata"); -> 이 부분에서 filedata는 기본 디폴더 이름으로 정해저 있다. 변경 하고 싶은면 Flex 에서 변경하고 ASP 부분에서도 Set oFile = abc("filedata")(1) 을 변경 해주어야 한다.
URLRequest에 관한 설명은 아래 링크를 가면 잘 나와있다.
http://flexdocs.kr/docs/flex2/langref/flash/net/URLRequest.html#data