A file that enables an ASPX page to accept a html,asp page transfer

Source: Internet
Author: User
Tags foreach config file upload file url html page split trim
Page

ASPX accepts the ASPX page file as simple as HtmlInputFile, but if you accept the HTML page post file
It is not good to do, I modeled the method of ASP as follows, their own testing through, take out to share, you can limit
File content, type, size, custom storage location, within Congfig.xml
Content of HTML page: (from FCKeditor)
<title>fckeditor-uploaders tests</title>
<script language= "JavaScript" >

function SendFile ()
{
var suploaderurl = Cmbuploaderurl.value;

if (suploaderurl.length = 0)
Suploaderurl = Txtcustomurl.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, custommsg)
{
Switch (errornumber)
{
Case 0://No errors
Txturl.value = FILEURL;
Alert (' File uploaded with no errors ');
break;
Case 1://Custom Error
alert (custommsg);
break;
Case://Custom Warning
Txturl.value = FILEURL;
alert (custommsg);
break;
Case 201:
Txturl.value = FILEURL;
Alert (' A file with the same name is already available. The uploaded file has been renamed to "' + FileName + '");
break;
Case 202:
Alert (' Invalid file ');
break;
Case 203:
Alert ("Security error.") You are probably don ' t have enough permissions to upload. Please check your server. ") ;
break;
Default:
Alert (' Error on file upload. Error number: ' + errornumber ';
break;
}
}

</script>
<body>
<table cellspacing= "0" cellpadding= "0" width= "100%" border= "0" height= "100%" >
<tr>
<td>
<table cellspacing= "0" cellpadding= "0" width= "100%" border= "0" >
<tr>
&LT;TD nowrap style= "height:43px" >
Select the "File Uploader" to use:<br>
<select id= "Cmbuploaderurl" name= "Select1" >
<option selected Value= "asp/upload.asp" >ASP</option>
<option value= "php/upload.php" >PHP</option>
<option value= "Upload.aspx?type=image" >aspx</option>
</select>
</td>
&LT;TD nowrap style= "height:43px" > </td>
&LT;TD width= "100%" style= "height:43px" >
Custom Uploader url:<br>
<input id= "Txtcustomurl" style= "width:100%; Background-color: #dcdcdc "Disabled type=" text >
</td>
</tr>
</table>
<br>
<table cellspacing= "0" cellpadding= "0" width= "100%" border= "0" >
<tr>
&LT;TD nowrap>
<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>
&LT;TD style= "width:16px" > </td>
&LT;TD valign= "Top" width= "100%" >
Uploaded File Url:<br>
<input id= "Txturl" style= "width:100%" readonly "text" >
</td>
</tr>
</table>
<br>
Post URL: <span id= "Eurl" > </span>
</td>
</tr>
<tr>
&LT;TD height= "100%" >
<iframe name= "Uploadwindow" width= "100%" height= "100%" ></iframe>
</td>
</tr>
</table>
</body>
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 + errornumber +", \ "" + fileurl.replace ("\", "\\\") + "\", \ "" + Filename.repl Ace ("\", "\\\") + "\", \ "" + custommsg.replace ("\" "," \\\ ") +" \ "); \ n");
Text. Append ("</script>");
Response.Write (text. ToString ());
Response.End ();
}
public void GetConfig (string type, out string[] allowedext, out string[] denyedext,out string savepath,out long MaxSize)
{
XmlDocument doc = new XmlDocument ();
Doc. Load (Server.MapPath (@ ". \config.xml"));
XmlElement Root=doc. DocumentElement;
XmlNodeList Imagenodelist=root. getElementsByTagName (type);
Allowedext = imagenodelist[0]. FirstChild.InnerText.Trim (). Split (' | ');
Denyedext = imagenodelist[0]. LastChild.InnerText.Trim (). Split (' | ');
Savepath = root. getElementsByTagName ("UserPath"). Item (0). Innertext.trim ();
Try
{
MaxSize = Convert.toint64 (root. getElementsByTagName ("MaxSize"). Item (0). Innertext.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 of Allowedext, out of Denyedext, out Savepath,out maxSize);
}
if (type = = "File")
{
GetConfig ("File", out of Allowedext, out of Denyedext, out of Savepath, out maxSize);
}
if (type = = "Flash")
{
GetConfig ("Flash", out of Allowedext, out of Denyedext, out of Savepath, out maxSize);
}
if (Savepath = = 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};//binary number

byte[] bytes = new byte[bytes1. Length + bytes2. Length];

Merging binary streams
MemoryStream ms = new MemoryStream (bytes);
Ms. Write (bytes1, 0, Bytes1. Length);
Ms. Write (bytes2, 0, Bytes2. Length);

int count = 0, pos = 0;
Start looking for 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 of the bytes byte array from 0 to the POS is the string you want
From POS + 5 to the end, that's 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 is:
"-----------------------------7d5fa3820f84\r\ncontent-disposition:form-data; Name=\ "Newfile\"; Filename=\ "F:\\documents\\4 (10995). jpg\" \r\ncontent-type:image/pjpeg\r\n\r\n
followed by file data
************************************************************************************************/
Head = head. ToLower ();
Head = head. Remove (0, head. IndexOf ("\ r \ n") + 2);
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\"; Filename=\ "F:\\documents\\4 (10995). jpg\"
FileType = head. Remove (0, postfilename.length + 3);//returns:content-type:image/pjpeg
Postfilename = postfilename.substring (Postfilename.indexof ("filename=") + "Filename=". Length);//c:\path\name
FileName = Path.getfilename (postfilename);
Fileext = filename.substring (Filename.lastindexof (".") + 1);
if (Filedata.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 (ResolveUrl (savepath + filename), filename, "Forrbiden");
Return
}
}

foreach (string ext in Allowedext) {
if (ext = = fileext) {Isallow = true; break;}
}
if (Isallow)
{
String tmppath = Server.MapPath (Savepath);
if (! Directory.Exists (Tmppath)) {
Try
{
Directory.CreateDirectory (Tmppath);
}
catch {sendresults ResolveUrl (savepath + filename), filename, "no write permission");}
}
Response.BinaryWrite (Filedata);
FileStream savefilestream= New FileStream (Tmppath+filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
for (long i = 0; i < filedata.longlength; i++)
{
Savefilestream.writebyte (Filedata[i]);
}
Savefilestream.close ();
Sendresults (0, ResolveUrl (savepath + filename), filename, "No errors");

}

}
}


Config.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<upload>
<enabled>true</enabled>
<userPath></userPath>
<maxSize>500000</maxSize><!--Unit is byte-->
<file>
<allow>zip|rar</allow>
<deny>php|php3|php5|phtml|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|dll|reg|cgi</deny>
</file>
<image>
<allow>jpg|gif|jpeg|png|bmp</allow>
<deny></deny>
</image>
<flash>
<allow>swf|fla</allow>
<deny></deny>
</flash>
</upload>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.