ASPX accepts the file of the ASPX page is very simple, just use HTMLINPUTFILE, it is fine, but if you accept the HTML page post file
It's not very easy to handle. I imitate the method of ASP as follows. Test it by myself and take it out for everyone to share. You can limit it.
File content, type, size, custom storage position, in conmfig.xml
Content of HTML page: (from fckeditor)
<html>
<head>
<Title> Fckeditor -Uploaders Tests </Title>
<script language = "javascript">
Function sendfile ()
{{
var suploaderurl = cmbuploaderurl.value;
If (Suploaderurl.Length == 0)
suploaderurl = txtcustomall.Value;
If (Suploaderurl.Length == 0)
{{
Alert ('Please PRovide Your Custom Url or Select a default one');
Return;
}
eURL.Innerhtml = Suploaderurl;
txtURL.VALUE = '';
frmupload.action = Suploaderurl;
frmupload.submit ();
}
Function OnuploadCompleted (ErrorNumber, Fileurl, FILENAME, CUSTMSSG)
{{
Switch (ERRORNUMBER)
{{
case 0: // no errors
txtURL.VALUE = Fileurl;
Alert ('File Uploaded with No Error');
Break;
Case 1: // Custom error
Alert (CUSTMSSG);
Break;
Case 10: // Custom Warning
txtURL.VALUE = Fileurl;
Alert (CUSTMSSG);
Break;
Case 201:
txtURL.VALUE = Fileurl;
Alert ('A File with The Same name is almedy available. The upload file has ben renamed to "' + filename + '"');
Break;
Case 202:
Alert ('Invalid File');
Break;
Case 203:
Alert ("Security Error. You Probably Don'T'T'T'T'T'T'T'T'T'T't enough permissions to upload. Please Check your server.");
Break;
DEFAULT:
alert ('Error On File upload. Eric Number:' + Errornumber);
Break;
}
}
</script>
</head>
<body>
<Table cellspacing = "0" cellpadding = "0">
<tr>
<TD>
<Table cellspacing = "0" cellpadding = "0">
<tr>
<TD Now4>
Select the "File Uploader" to use: <br>
<select id = "cmbuploaderurl" name = "select1">
<option select value = "asp/upload.asp"> ASP </option>
<option value = "php/upload.php"> pHP </option>
<option value = "upload.aspx? Type = Image"> ASPX </option>
</select>
</td>
<TD now44 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; </td>
<TD>
Custom uploader url: <br>
<input id = "txtCustomall" disabled type = "text">
</td>
</tr>
</table>
<br>
<Table cellspacing = "0" cellpadding = "0">
<tr>
<TD Now4>
<FORM ID = "FRMUPLOAD" TARGET = "UPLOADWINDOW" Enctype = "Multipart/Form-Data" action = "" Method = "Post">
upload a new file: <br>
<input type = "file" name = "newfile"> <br>
<input type = "Button" Value = "Send it to the server">
</form>
</td>
<TD> & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; </td>
<td value = "top">
uploaded file url: <br>
<input ID = "txtURL" Readonly Type = "Text">
</td>
</tr>
</table>
<br>
Post url: <span id = "eURL"> & nbsp; </spsp; </sps>
</td>
</tr>
<tr>
<TD>
<ifme name = "uploadwindow"> </iframe>
</td>
</tr>
</table>
</body>
</html>
content of upload.aspx:
<%@ page language = "c#" autoeventwireup = "true" codefile = "upload.aspx.cs" inherits = "upload"%>%>
The following is the background code:
using system;
using system.data;
using system.configuration;
using system.collections;
using system.io;
using system.text;
using system.web;
USING SYSTEM.WEB.SECURITY;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlControls;
using system.xml;
USING SYSTEM.COLLECTIONS.SPECIALIZED;
Public Partial Class Upload: 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 ());
response.end ();
}
Public Void Get Getfig (String Type, Out String [] Allowedext, Out String [] Denyedext, Out Long MaxSize), Out Long MaxSize)
{{
xmldocumet doc = new xmldocument ();
doc.load (server.mappath (@"./ config.xml");
xmlement root = doc.documentelement;
XMLNODELIST ImageExodelist = root.GetelementsBytagname (Type);
Allowedext = ImageNodelist [0] .firstChild.innertext.trim (). split ('|');
Denyedext = ImageNodelist [0] .lastchild.innertext.trim (). split ('|');
savepath = root.getelementsbybytagname ("userpath"). item (0) .innertext.trim ();
try
{{
maxsize = convert.toint64 (root.getelementsBytagname ("maxsize"). item (0) .innertext.trim ()););););););); )Reeext.trim ());););););););); )Reeext.trim ());););););););); )Reeext.trim ()););););;);
}
catch {maxsize = 10*1024;}
}
Protected void Page_Load (Object Sender, EventArgs E)
{{
string [] allowedext = New String [] {}, Denyedext = New String [] {};
string savepath = string.empty;
long maxsize = 10000;
string type = request.querystring ["type"];
if (Type! = Null && Type! = String.empty)
Type = Type.tolower ();
else
type = "file";
if (Type == "Image")
{{
getConfig ("Image", Out Allowedext, Out Denyedext, Out Savepath, Out MaxSize);
}
if (Type == "File")
{{
getConfig ("file", out allowedext, out deenyedext, out savepath, out maxsize);
}
if (Type == "Flash"))
{{
getConfig ("Flash", Out Allowedext, Out Denyedext, Out Savepath, Out MaxSize);
}
ifPath == String.empty || Savepath == "")
savepath = "~/userfiles/";
if (! Savepath.endswith ("/") Savepath+= "/";
/*************************************************** ***************************************
byte [] bytes1 = System.text.encoding.Default.getbytes ("This is a string/n/n/n/n");
byte [] bytes2 = New byte [] {1, 33, 23, 3, 0, 56, 55, 235, 5}; //
byte [] bytes = New byte [bytes1.length + bytes2.length];
// Merge binary stream
Memorystream MS = New Memorystream (bytes);
ms.write (bytes1, 0, bytes1.length);
ms.write (bytes2, 0, bytes2.length);
int count = 0, pos = 0;
// Start find four '/n'
for (int i = 0; I <bytes.length; i ++)
{{
if (bytes [i] == (int) '/n')
{{
Count ++;
if (count == 4)
{{
POS- = 4;
Break;
}
}
}
if (count == 4)
{{
// Here, the position from 0 to POS in the Bytes byte array is the string you want
// From the beginning of POS + 5 to the end, the binary you want
}
************************************************* *******************************************
byte [] Filedata, FormData;
FormData = Request.binaryRead (request.contentLength);
string head = string.empty;
encoding encoding = encoding.utf8;
long 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] == (byte) '/n')
{{
pos = i;
Break;
}
}
if (pOS == 0) {response.end (); return;}
head = encoding.getstring (formdata, 0, (int) pos);
Filedata = New byte [FormData.longlength -POS -3];
Array.copy (formdata, pos + 4, FileData, 0, FormData.longlength -POS -4);
/*************************************************** ***************************************************
// The form of the form comes is:
//"-----------------------------7d5fa3820f84/r/ncontent-disposition: form-data; name=/"newfile/ ".
// Later is the file data
************************************************* **************************************************************
head = head.tolower ();
head = head.remove (0, head.indexof ("/r/n");
head = head.replace ("/" "," ");
string postfilename = string.empty;
string filename; // no path
string 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); // Return: Content-Type: Image/PJPEG
Postfilename = Postfilename.substring (Postfilename.indexof ("Filename =") + "FILENAME =". LENGTH); // C:/Path/Name
filename = path.getfilename (postfilename);
filext = filename.substring (filename.lastindexof (".") + 1;
ifdata.longlength> maxsize {{
Sendresults (2, ResolveURL (Savepath + Filename), FILENAME, "Too Large");
Return;
}
BOOL Isallow = FALSE;
FOREACH (String Ext in Denyedext) {
if (ext == fileExt) {
Isallow = false;
Sendresults (202, Resolveurl (SavePath + FILENAME), FILENAME, "FORBIDEN");
Return;
}
}
Foreach (String EXT in AllowExt) {{
if (ext == fileExt) {isallow = true; break;}
}
if (ISALLOW)
{{
string tmppath = server.mappath (savepath);
if (! Directory.exists (tmppath)) {{
try
{{
Directory.createDirectory (TMPPATH);
}
Catch {Sendresults (200, ResolveUrl (Savepath + FILENAME), FILENAME, "No Write Permissions");}
}
//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, "No ERRORS");
}
}
}
config.xml
<? xml version = "1.0" encoding = "UTF-8"?>
<upload>
<Evabled> True </enabled>
<userpath> </userpath>
<maxsize> 500000 </maxsize> <!-Unit is byte->
<file>
<allow> Zip | RAR </all>
<DENY> PHP | PHP3 | PHP5 | PHTML | ASP | ASPX | ASCX | JSP | CFM | CFM | Pl | Bat | EXE | DLL | Regi | CGI </deeny>
</file>
<Image>
<allow> jpg | gif | jpeg | png | bmp </allow>
<DENY> </Deny>
</iMage>
<flash>
<allow> SWF | FLA </allow>
<DENY> </Deny>
</flash>
</upload>