ASPX는 ASPX 페이지의 파일이 매우 간단합니다. htmlinputfile 만 사용하면 괜찮지 만 HTML 페이지 게시물 파일을 허용하는 경우
처리하기가 쉽지 않습니다.
conmfig.xml에서 파일 컨텐츠, 유형, 크기, 사용자 정의 스토리지 위치
HTML 페이지의 내용 : (Fckeditor에서)
<html>
<헤드>
<title> fckeditor -uploaders tests </title>
<script language = "javaScript">
함수 sendfile ()
{{
var suploaderurl = cmbuploaderurl.value;
if (suploaderurl.length == 0)
suploaderurl = txtCustomAll.Value;
if (suploaderurl.length == 0)
{{
알림 ( '사용자 정의 URL을 제공하거나 기본 URL을 선택하십시오');
반품;
}
eurl.innerhtml = suploaderurl;
txturl.value = '';
frmupload.action = suploaderUrl;
frmupload.submit ();
}
ONUPLoadCompleted (Errornumber, FileUrl, Filename, Custmssg)
{{
스위치 (Errornumber)
{{
사례 0 : // 오류 없음
txturl.value = fileUrl;
Alert ( '오류없이 업로드 된 파일');
부서지다;
사례 1 : // 사용자 정의 오류
경고 (custmssg);
부서지다;
사례 10 : // 사용자 정의 경고
txturl.value = fileUrl;
경고 (custmssg);
부서지다;
사례 201 :
txturl.value = fileUrl;
ALERT ( '이름이 같은 파일은 AlMedy를 사용할 수 있습니다. 업로드 파일의 이름이 "' + filename + '"'로 이름이 바뀌 었습니다.
부서지다;
사례 202 :
알림 ( '유효하지 않은 파일');
부서지다;
사례 203 :
ALERT ( "보안 오류. 업로드 할 권한이 충분하지 않아도 될 것입니다. 서버를 확인하십시오.");
부서지다;
기본:
ALERT ( '파일 업로드의 오류. ERIC 번호 :' + errornumber);
부서지다;
}
}
</스크립트>
</head>
<body>
<테이블 cellspacing = "0"cellPadding = "0">
<tr>
<td>
<테이블 cellspacing = "0"cellPadding = "0">
<tr>
<td now4>
사용할 "파일 업 로더"를 선택할 : <br>
<select id = "cmbuploaderUrl"name = "select1">
<옵션 select value = "asp/upload.asp"> asp </옵션>
<옵션 값 = "php/upload.php"> php </옵션>
<옵션 값 = "upload.aspx? type = image"> aspx </옵션>
</선택>
</td>
<nowsp; & nbsp;
<td>
사용자 정의 업로더 URL : <br>
<input id = "txtCustomAll"disabled type = "text">
</td>
</tr>
</테이블>
<br>
<테이블 cellspacing = "0"cellPadding = "0">
<tr>
<td now4>
<form id = "frmupload"target = "uploadwindow"enctype = "multipart/form-data action =" "method ="post ">
새 파일을 업로드하십시오 : <br>
<입력 유형 = "파일"이름 = "NewFile"> <br>
<입력 유형 = "버튼"value = "서버로 보내기">
</form>
</td>
<td> & nbsp;
<td value = "top">
업로드 된 파일 URL : <br>
<input id = "txturl"readonly type = "text">
</td>
</tr>
</테이블>
<br>
Post URL : <span id = "Eurl"> & nbsp;
</td>
</tr>
<tr>
<td>
<ifme name = "uploadwindow"> </iframe>
</td>
</tr>
</테이블>
</body>
</html>
upload.aspx의 내용 :
<%@ page language = "c#"autoeventwireup = "true"codefile = "upload.aspx.cs"inherits = "Upload"%>%>
다음은 배경 코드입니다.
시스템 사용;
System.Data 사용;
System.configuration 사용;
시스템 사용;
System.io 사용;
system.text 사용;
System.Web 사용;
System.web.security 사용;
system.web.ui 사용;
system.web.ui.webcontrols 사용;
system.web.ui.webcontrols.webparts 사용;
system.web.ui.htmlcontrols 사용;
System.xml 사용;
시스템 사용.
공개 부분 클래스 업로드 : System.Web.ui.Page
{{
public void sendResults (int errornumber, String fileUrl, String filename, String custommsg)
{{
StringBuilder text = new StringBuilder ();
text.append ( "<script type =/"text/javaScript/">">);
text.append ( "window.parent.onuploadCompleted (" + errorrnumber + ",/" + fileUrl.replace ( "/", "//" "),/" + filename.replace ( "/" ","// /"")) + "/",/" + custommsg.replace ("/"", "//" ");/n");/n ");/n");/n);/ n ");/n");/n ");/n");/n ";
text.append ( "</script>");
response.write (text.toString ());
응답 ();
}
public void getfig (문자열 유형, Out String [] allendext, out String [] denyedext, out long maxsize), out maxsize)
{{
xmldocumet doc = new xmldocument ();
doc.load (server.mappath (@"./ config.xml");
xmlement root = doc.documentElement;
xmlnodelist imageexodelist = root.getElementsByTagName (type);
allessext = imageNodelist [0] .firstChild.innerText.trim ();
denyedext = imageNodelist [0] .lastChild.innerText.trim ();
SavePath = root.getElementsByByTagName ( "USERPATH"). innerText.trim ();
노력하다
{{
maxsize = convert.toint64 (root.getElementsByTagName ( "maxSize"). item (0). innerText.trim ()););););););); ;);););););););););););
}
잡기 {maxsize = 10*1024;}
}
보호 된 void page_load (객체 발신자, EventArgs e)
{{
문자열 [] allendext = new String [] {}, denyedext = new String [] {};
String savepath = string.empty;
긴 최대 크기 = 10000;
문자열 유형 = request.queryString [ "type"];
if (type! = null && type! = string.empty)
type = type.tolower ();
또 다른
type = "파일";
if (type == "image")
{{
getConfig ( "image", out allendext, out denyedext, out savepath, out maxsize);
}
if (type == "file")
{{
getConfig ( "파일", outlendext, out deenyedext, out savepath, out maxsize);
}
if (type == "flash"))
{{
getConfig ( "Flash", OUT ALLESTEXT, OUT DENYEDEXT, OUT SAVEPPATH, Out MaxSize);
}
ifpath == string.empty ||
savepath = "~/userfiles/";
if (! savepath.endswith ( "/") savepath+= "/";
/******************************************* ** ***********************************
BYTE [] bytes1 = system.text.encoding.default.getBytes ( "이것은 문자열/n/n/n/n");
BYTE [] BYTES2 = 새로운 바이트 [] {1, 33, 23, 3, 0, 56, 55, 235, 5};
바이트 [] bytes = new Byte [bytes1.length + bytes2.length];
// 이진 스트림을 병합합니다
MemoryStream MS = New MemoryStream (바이트);
Ms.Write (bytes1, 0, bytes1.length);
Ms.Write (bytes2, 0, bytes2.length);
int count = 0, pos = 0;
// 4 '/n'찾기 시작
for (int i = 0; i <bytes.length; i ++)
{{
if (bytes [i] == (int) '/n')
{{
카운트 ++;
if (count == 4)
{{
pos- = 4;
부서지다;
}
}
}
if (count == 4)
{{
// 여기, 바이트 바이트 배열의 0에서 pos 위치는 원하는 문자열입니다.
// POS + 5의 시작부터 끝까지 원하는 이진
}
****************************************** ****************************************
바이트 [] filedata, formdata;
formdata = request.binaryread (request.contentLength);
문자열 head = string.empty;
인코딩 인코딩 = encoding.utf8;
긴 pos = 0;
for (long i = 0; i <formdata.longlength; i ++)
{{
if (formData [i] == (byte) '/r'&& formdata [i + 1] == (byte) '/n'&& formdata [i + 2] == (byte) '/r'&& formdata [i + 3] == (바이트) '/n')
{{
pos = i;
부서지다;
}
}
if (pos == 0) {response.end ();
head = encoding.getString (formdata, 0, (int) pos);
filedata = 새로운 바이트 [formdata.longlength -pos -3];
Array.copy (FormData, POS + 4, FileData, 0, FormData.Longlength -pos -4);
/******************************************* *********************************************** ***
// 양식의 형식은 다음과 같습니다.
// "--------------------------------- 7d5fa3820f84/r/ncontent-disposition : form-data; name =/"newfile/ ".
// 나중에 파일 데이터입니다
****************************************** ***************************************************************************************** 누군가 ***********
head = head.tolower ();
head = head.remove (0, head.indexof ( "/r/n");
head = head.replace ( "/" "," ");
문자열 postfilename = string.empty;
문자열 filename; // 경로 없음
문자열 filetype, fileExt;
postfilename = head.substring (0, head.indexof ( "/r/n"); // content-disposition : form-data; name =/"newfile/"; 4 (10995) .jpg/"
filetype = head.remove (0, postfilename.length + 3);
postfilename = postfilename.substring (postfilename.indexof ( "filename =") + "filename =". length);
filename = path.getfilename (postfilename);
filext = filename.substring (filename.lastindexof ( ".") + 1;
ifdata.longlength> maxsize {{
sendResults (2, resolveUrl (SavePath + filename), 파일 이름, "너무 큰");
반품;
}
bool isallow = false;
foreach (denyedext in string ext) {
if (ext == fileExt) {
isalow = 거짓;
SendResults (202, ResolveUrl (SavePath + Filename), Filename, "Forbiden");
반품;
}
}
foreach (string ext in allestext) {{
if (ext == fileExt) {isalow = true;}
}
if (isallow)
{{
문자열 tmppath = server.mappath (savepath);
if (! directory.exists (tmppath)) {{
노력하다
{{
directory.createdirectory (tmppath);
}
catch {sendResults (200, resolveUrl (savepath + filename), 파일 이름, "쓰기 권한 없음";}
}
//response.binaryWrite (FileData);
FILESTREAM SAVEFILESTREAM = NEW FILESTREAM (TMPPATH+FILENAME, FILEMODE.OPENORCREATE, FILEACCESS.READWRITE);
for (long i = 0; i <fledata.longlength; i ++)
{{
SaveFilestream.writeByte (FileData [i]);
}
SaveFilestream.close ();
sendResults (0, resolveUrl (SavePath + filename), Filenamame, "오류 없음");
}
}
}
config.xml
<? xml 버전 = "1.0"encoding = "utf-8"?>
<업로드>
<Evabled> True </enabled>
<UserPath> </userPath>
<maxsize> 500000 </maxsize> <!-단위는 바이트->입니다
<파일>
<허용> zip |. rar </all>
<Php3 |
</file>
<Image>
<jpg | jpeg |
<deny> </deny>
</image>
<Flash>
<허용> swf |
<deny> </deny>
</flash>
</업로드>