ASPX accepte que le fichier de la page ASPX est très simple, utilisez simplement htmlinputfile, c'est bien, mais si vous acceptez le fichier de publication de la page HTML
Ce n'est pas très facile à gérer.
Contenu de fichier, type, taille, position de stockage personnalisée, dans conmfig.xml
Contenu de la page HTML: (de fckEditor)
<html>
<adal>
<Title> FcKEditor -Uploaders Tests </Title>
<script linguisse = "javascript">
Fonction sendFile ()
{{
var suplloaderurl = cmbuploadherUrl.value;
If (Suploaderurl.length == 0)
Suploaderurl = txtCustomall.Value;
If (Suploaderurl.length == 0)
{{
Alert ('Veuillez fournir votre URL personnalisée ou sélectionner une par défaut');
Retour;
}
eurl.innerhtml = suplloaderurl;
txtul.value = '';
FrMupload.Action = Suploaderurl;
FrMupload.Submit ();
}
Fonction onuploadCompleted (errornumber, fileurl, nom de fichier, custmssg)
{{
Switch (errorNumber)
{{
cas 0: // pas d'erreurs
txtul.value = fileurl;
Alert ('fichier téléchargé sans erreur');
Casser;
Cas 1: // Erreur personnalisée
Alerte (CustMSSG);
Casser;
Cas 10: // Avertissement personnalisé
txtul.value = fileurl;
Alerte (CustMSSG);
Casser;
Cas 201:
txtul.value = fileurl;
Alert ('Un fichier avec le même nom est disponible.
Casser;
Cas 202:
Alert («fichier non valide»);
Casser;
Cas 203:
Alert ("Erreur de sécurité. Vous ne faites probablement pas assez de permis pour télécharger. Veuillez vérifier votre serveur.");
Casser;
DÉFAUT:
alert ('Erreur sur le téléchargement de fichiers. Numéro Eric:' + errorNumber);
Casser;
}
}
</cript>
</ head>
<body>
<Table cellSpacing = "0" cellpadding = "0">
<tr>
<TD>
<Table cellSpacing = "0" cellpadding = "0">
<tr>
<Td maintenant4>
Sélectionnez le "File Uploader" à utiliser: <br>
<select id = "cmbuploadherUrl" name = "select1">
<option select value = "asp / upload.asp"> asp </opoption>
<option valeur = "php / upload.php"> php </opoption>
<Option Value = "upload.aspx? Type = Image"> Aspx </opoption>
</lect>
</td>
<TD maintenant 44 & nbsp;
<TD>
URL du téléchargement personnalisé: <br>
<input id = "txtcustomall" Désable Type = "Text">
</td>
</tr>
</ table>
<br>
<Table cellSpacing = "0" cellpadding = "0">
<tr>
<Td maintenant4>
<Form id = "frmupload" cible = "uploadwindow" enctype = "multipart / form-data" action = "" method = "post">
Téléchargez un nouveau fichier: <br>
<input type = "file" name = "newfile"> <br>
<entrée type = "bouton" value = "Envoyez-le au serveur">
</ form>
</td>
<Td> & nbsp;
<td value = "top">
URL du fichier téléchargé: <br>
<input id = "txtul" readonly type = "text">
</td>
</tr>
</ table>
<br>
URL post: <span id = "Eurl"> & nbsp;
</td>
</tr>
<tr>
<TD>
<ifme name = "uploadwindow"> </ iframe>
</td>
</tr>
</ table>
</docy>
</html>
Contenu de upload.aspx:
<% @ page linguistique = "C #" AutoEventWireup = "true" codefile = "upload.aspx.cs" hérites = "upload"%>%>
Ce qui suit est le code d'arrière-plan:
Utilisation du système;
Utilisation de System.Data;
Utilisation de System.Configuration;
Utilisation de System.Collections;
Utilisation de System.io;
Utilisation de System.Text;
Utilisation de System.Web;
Utilisation de System.Web.Security;
Utilisation de System.Web.UI;
Utilisation de System.Web.Ui.WebControls;
Utilisation de System.Web.Ui.WebControls.WebParts;
Utilisation de System.Web.Ui.htmlControls;
Utilisation de System.xml;
Utilisation de System.Collections. spécialized;
Téléchargement public partiel des cours: System.web.ui.page
{{
Public void sendResults (int errorNumb, 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 ("</cript>");
réponse.Write (text.toString ());
réponse.end ();
}
Public void get getFig (Type de chaîne, string out [] alloweDext, out String [] denyedex
{{
xmlDocumet doc = new XMLDocument ();
doc.load (server.mappath (@ "./ config.xml");
xmlement root = doc.DocumentElement;
XmlNodeList imageExodeList = root.getElementsByTagName (type);
Allowext = ImageNodeList [0] .FirstChild.innerText.trim ().
Denyedext = ImageNodeList [0] .lastChild.innerText.trim ().
SavePath = root.getElementsByTagName ("UserPath").
essayer
{{
maxSize = convert.toint64 (root.getElementsByTagName ("MaxSize"). Item (0) .InnerText.trim ()););););););); ;);););););) Reext.trim ());););););););
}
Catch {maxSize = 10 * 1024;}
}
Protégé void page_load (expéditeur d'objet, 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 ();
autre
type = "fichier";
if (type == "image")
{{
getConfig ("image", out perteext, out denyedext, out SavePath, out maxsize);
}
if (type == "fichier")
{{
getConfig ("fichier", out allowext, out deenyedext, out SavePath, out maxsize);
}
if (type == "flash"))
{{
getConfig ("flash", out allowext, out denyedext, out SavePath, out maxsize);
}
ifpath == String.empty ||
SavePath = "~ / UserFiles /";
if (! SavePath.endswith ("/") SavePath + = "/";
/ ************************************************* ** ***************************************
Byte [] bytes1 = System.Text.Encoding.default.getBytes ("Ceci est une chaîne / n / n / n / n");
octet [] bytes2 = nouveau octet [] {1, 33, 23, 3, 0, 56, 55, 235, 5};
octet [] bytes = new byte [bytes1.length + bytes2.length];
// fusionner le flux binaire
MemoryStream ms = new MemoryStream (octets);
Ms.Write (Bytes1, 0, bytes1.length);
Ms.Write (Bytes2, 0, Bytes2.Length);
int count = 0, pos = 0;
// commence à trouver quatre '/ n'
pour (int i = 0; i <bytes.length; i ++)
{{
if (octets [i] == (int) '/ n')
{{
Count ++;
if (count == 4)
{{
Pos- = 4;
Casser;
}
}
}
if (count == 4)
{{
// Ici, la position de 0 à POS dans le tableau d'octets d'octets est la chaîne que vous voulez
// Du début de Pos + 5 à la fin, le binaire que vous voulez
}
************************************************* * ********************************************
octet [] filedata, formdata;
FormData = request.binaryRead (request.ContentLength);
String head = string.empty;
codage codage = codage.utf8;
long pos = 0;
pour (long i = 0; i <formdata.longLength; i ++)
{{
if (formData [i] == (byte) '/ r' && formData [i + 1] == (byte) '/ n' && formdata [i + 2] == (byte) '/ r' && formdata [i i + 3] == (byte) '/ n')
{{
pos = i;
Casser;
}
}
if (pos == 0) {réponse.end ();}}
head = coding.getString (formdata, 0, (int) pos);
Filedata = new byte [formdata.longLength -pos -3];
Array.copy (formdata, pos + 4, filedata, 0, formData.longLength -pos -4);
/ ************************************************* ** ************************************************** ***
// La forme du formulaire vient est:
// "------------------------- ".
// plus tard est les données du fichier
************************************************* * **************************************************** ***********
head = head.tolower ();
head = head.remove (0, head.indexof ("/ r / n");
head = head.replace ("/" "," ");
String postFileName = String.empty;
Nom de fichier de chaîne; // pas de chemin
String FileType, fileXt;
PostFileName = head.substring (0, head.indexof ("/ r / n"); // contenu-disposition: form-data; name = / "newfile /"; 4 (10995) .jpg / "
FileType = head.reMove (0, postFileName.Length + 3);
PostFileName = PostFileName.SubString (PostFileName.Indexof ("FileName =") + "FileName =". Longueur);
filename = path.getFileName (postFileName);
fileXt = filename.substring (filename.lastindexof (".") + 1;
ifdata.longLength> maxsize {{
SendResults (2, ResolveUrl (SavePath + FileName), nom de fichier, "trop grand");
Retour;
}
Bool isallow = false;
Foreach (String ext dans denyedext) {
if (ext == fileext) {
Isallow = false;
SendResults (202, ResolveUrl (SAVEPATH + FILENAME), nom de fichier, "Forbiden");
Retour;
}
}
Foreach (String ext dans allowExt) {{
if (ext == fileXt) {Isallow = true;}}
}
si (isallow)
{{
String tmpppath = server.mappath (SavePath);
if (! Directory.exists (tmpppath)) {{
essayer
{{
Directory.CreateDirectory (TMPPATH);
}
Catch {sendReSults (200, ResolveUrl (SavePath + FileName), nom de fichier, "No Write Autorisations");}
}
//Response.BinaryWrite (FileData);
FileStream SaveFileStream = new FileStream (TMPPATH + FileName, fileMode.OpenorCreate, FileAccess.readwrite);
pour (long i = 0; i <fledata.longLength; i ++)
{{
SaveFileStream.WriteByte (Filedata [i]);
}
SaveFileStream.Close ();
SendResults (0, ResolveUrl (SAVEPATH + FILENAME), FileNaMame, "pas d'erreurs");
}
}
}
config.xml
<?
<Lipload>
<soabled> true </velable>
<UserPath> </ userPath>
<MAXSIZE> 500000 </MAXSIZE> <! - L'unité est octet->
<fichier>
<permettre> zip | rar </tim>
«Dente> PHP |
</ file>
<image>
<permettre> JPG | GIF |
<seny> </ deny>
</ image>
<flash>
<permettre> SWF |
<seny> </ deny>
</ flash>
</ téléchargement>