ASP instance: A simple ASP upload class without components

Source: Internet
Author: User
Tags date file upload return servervariables

Simple ASP no component upload class, sent to let everyone see. Can do the experiment!

The following are the referenced contents:

<%@ language= "JavaScript"%>
<%
var self = request.servervariables ("Script_name");
if (Request.ServerVariables ("request_method") = = "POST")
{
var oo = new UploadFile ();
Oo.path = "MyFile"; Holds the path, is null represents the current path, defaults to UploadFile
oo.named = "File"; Named by date, which is named with the name, and file represents the filename itself, the default is file
Oo.ext = "All"; Allow uploaded extensions, all representations are allowed, default to all
Oo.over = true; Is overridden when the same file name exists, and defaults to False
Oo.size = 1*1024*1024; Maximum number of bytes limit, default to 1G
Oo.upload ();
Response.Write (' <script type= "Text/javascript" >location.replace ("' +self+ ') </script> ');
}

ASP no component Upload class
function UploadFile ()
{
var blen = request.totalbytes;
var btext = Request.BinaryRead (Blen);
var oo = Server.CreateObject ("ADODB.") Stream ");
Oo.mode = 3;
This.path = "UploadFile";
this.named = "File";
This.ext = "All";
This.over = false;
This.size = 1*1024*1024*1024; 1GB

File Upload
This.upload = function ()
{
var o = This.getinfo ();
if (o.size>this.size)
{
Alert ("The file is too large to upload!");
Return
}
var f = this.getfilename ();
var ext = f.replace (/^.+\./, "");
if (this.ext!= "All" &&!new RegExp (This.ext.replace (/,/g, "|"), "IG"). Test (EXT)
{
Alert ("Currently does not support the extension of" +ext+ file Upload! ");
Return
}
if (this.named== "date")
{
f = new Date (). toLocaleString (). Replace (/\d/g, "") + "." + ext;
}

Oo.open ();
Oo.type = 1;
Oo.write (O.bin);
This.path = This.path.replace (/[^\/\\]$/, "$&/");
var fso = Server.CreateObject ("Scripting.FileSystemObject");
if (this.path!= "" &&!fso.folderexists (Server.MapPath (This.path))
{
Fso.createfolder (Server.MapPath (This.path));
}
Try
{
Oo.savetofile (Server.MapPath (this.path+f), this.over?2:1);
Alert ("Upload success!");
}
catch (E)
{
Alert ("Sorry, this file already exists!") ");
}
Oo.close ();
Delete (OO);

}

Get binary and File bytes
This.getinfo = function ()
{
Oo.open ();
oo.type=1;
Oo.write (Btext);
oo.position = 0;
oo.type=2;
oo.charset= "Unicode";
var gbcode=escape (Oo.readtext ()). Replace (/%u (..) (..) /g, "%$2%$1");
var spos=gbcode.indexof ("%0d%0a%0d%0a") +12;
var slength=blen-(gbcode.substring (0,gbcode.indexof ("%0d%0a")). Length/3)-spos/3-6;
Oo.close ();

Oo.open ();
Oo.type = 1;
Oo.write (Btext);
OO.POSITION=SPOS/3;
var bfile=oo.read (slength);
Oo.close ();

return {bin:bfile, size:slength};
}

Get file name
This.getfilename = function ()
{
Oo.open ();
Oo.type = 2;
Oo.writetext (Btext);
oo.position = 0;
Oo.charset = "gb2312";
var fileName = Oo.readtext (). Match (/filename=\) (. +?) \/i) [1].split ("\"). Slice (-1) [0];
Oo.close ();
return fileName;
}

Function alert (msg)
{
Response.Write (' <script type= "Text/javascript" >alert ("' +msg+ ');</script> ');
}
}
%>
<title>asp No component upload class </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<form action= "<%=self%>" "method=" Post "enctype=" Multipart/form-data "onsubmit=" Return (this.upfile.value!= '');" >
<input type= "File" name= "Upfile"/>
<input type= "Submit" value= "Upload file"/>
</form>
</body>




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.