ASP code and example of upload file with progress bar

Source: Internet
Author: User

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<%
Dim Sundyupload_sourcedata
Class Sundyupload
Dim objform,objfile,version,objprogress
Dim xmlpath,charsetencoding
Public Function Form (strform)
Strform=lcase (Strform)
If not objform.exists (strform) Then
Form= ""
Else
Form=objform (Strform)
End If
End Function

Public Function File (strfile)


Strfile=lcase (strfile)


If not objfile.exists (strfile) Then


Set file=new FileInfo


Else


Set File=objfile (strfile)


End If


End Function





Public Sub Uploadinit (progressxmlpath,charset)


Dim Requestdata,sstart,crlf,sinfo,iinfostart,iinfoend,tstream,istart,thefile


Dim Ifilesize,sfilepath,sfiletype,sformvalue,sfilename


Dim Ifindstart,ifindend


Dim Iformstart,iformend,sformname





version= "Upload Width Progress Bar Version 1.0"


Set objform=server.createobject ("Scripting.Dictionary")


Set objfile=server.createobject ("Scripting.Dictionary")


If request.totalbytes&lt;1 Then Exit Sub


Set Tstream = Server.CreateObject ("ADODB.stream")


Set sundyupload_sourcedata = Server.CreateObject ("ADODB.stream")


Sundyupload_sourcedata.type = 1


Sundyupload_sourcedata.mode =3


Sundyupload_sourcedata.open





Dim TotalBytes


Dim chunkreadsize


Dim Datapart, Partsize


Dim objprogress





TotalBytes = Request.TotalBytes ' Total size


Chunkreadsize = 64 * 1024 ' chunking size 64K


Bytesread = 0


Xmlpath = Progressxmlpath


Charsetencoding = CharSet


If charsetencoding = "" Then


charsetencoding = "Utf-8"


End If


Set objprogress = New Progress


Objprogress.progressinit (Xmlpath)


Objprogress.updateprogress totalbytes,0


' Circular chunking Read


Do While Bytesread &lt; totalbytes


' Chunk-Reading


Partsize = Chunkreadsize


If partsize + bytesread &gt; totalbytes Then partsize = Totalbytes-bytesread


Datapart = Request.BinaryRead (partsize)


Bytesread = Bytesread + partsize





Sundyupload_sourcedata.write Datapart





Objprogress.updateprogress Totalbytes,bytesread


Loop


' Sundyupload_sourcedata.write Request.BinaryRead (request.totalbytes)


Sundyupload_sourcedata.position=0


RequestData =sundyupload_sourcedata.read





Iformstart = 1


Iformend = LenB (requestdata)


Crlf = ChrB (+) &amp; ChrB (10)


Sstart = MidB (requestdata,1, InStrB (IFORMSTART,REQUESTDATA,CRLF)-1)


IStart = LenB (Sstart)


Iformstart=iformstart+istart+1


while (Iformstart +) &lt; Iformend


Iinfoend = InStrB (Iformstart,requestdata,crlf &amp; Crlf) +3


Tstream.type = 1


Tstream.mode =3


Tstream.open


Sundyupload_sourcedata.position = Iformstart


Sundyupload_sourcedata.copyto Tstream,iinfoend-iformstart


tstream.position = 0


Tstream.type = 2


Tstream.charset =charsetencoding


Sinfo = Tstream.readtext


Tstream.close


' Get form Item name


Iformstart = InStrB (Iinfoend,requestdata,sstart)


Ifindstart = InStr (22,sinfo, "name=" "", 1) +6


Ifindend = InStr (Ifindstart,sinfo, "" "", 1)


Sformname = LCase (Mid (Sinfo,ifindstart,ifindend-ifindstart))


' If it's a file


If InStr (45,sinfo, "filename=" "", 1) &gt; 0 Then


Set thefile=new FileInfo


' Get filename


Ifindstart = InStr (Ifindend,sinfo, "filename=" "", 1) +10


Ifindend = InStr (Ifindstart,sinfo, "" "", 1)


sFileName = Mid (Sinfo,ifindstart,ifindend-ifindstart)


Thefile.filename=getfilename (sFileName)


Thefile.filepath=getfilepath (sFileName)


' Get file type


Ifindstart = InStr (Ifindend,sinfo, "Content-type:", 1) +14


Ifindend = InStr (IFINDSTART,SINFO,VBCR)


Thefile.filetype =mid (Sinfo,ifindstart,ifindend-ifindstart)


Thefile.filestart =iinfoend


Thefile.filesize = iformstart-iinfoend-3


Thefile.formname=sformname


If not objfile.exists (sformname) Then


Objfile.add Sformname,thefile


End If


Else


' If it is a form item


Tstream.type =1


Tstream.mode =3


Tstream.open


Sundyupload_sourcedata.position = Iinfoend


Sundyupload_sourcedata.copyto tstream,iformstart-iinfoend-3


tstream.position = 0


Tstream.type = 2


Tstream.charset = charsetencoding


Sformvalue = Tstream.readtext


Tstream.close


If objform.exists (sformname) Then


Objform (Sformname) =objform (sformname) &amp; "," &amp;sformvalue


Else


Objform.add Sformname,sformvalue


End If


End If


Iformstart=iformstart+istart+1


Wend


Requestdata= ""


Set Tstream = Nothing


End Sub


Private Sub Class_Initialize





End Sub





Private Sub Class_Terminate


If request.totalbytes&gt;0 Then


Objform.removeall


Objfile.removeall


Set objform=nothing


Set objfile=nothing


Sundyupload_sourcedata.close


Set Sundyupload_sourcedata = Nothing


End If


Set objprogress = Nothing


Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")


If objfso.fileexists (Xmlpath) Then


objFSO.DeleteFile (Xmlpath)


End If


Set objFSO = Nothing


End Sub





Private Function GetFilePath (FullPath)


If fullpath &lt;&gt; "" Then


GetFilePath = Left (Fullpath,instrrev (FullPath, ""))


Else


GetFilePath = ""


End If


End Function





Private Function GetFileName (FullPath)


If fullpath &lt;&gt; "" Then


GetFileName = Mid (Fullpath,instrrev (FullPath, "") +1)


Else


GetFileName = ""


End If


End Function


End Class

Class FileInfo


Dim Formname,filename,filepath,filesize,filetype,filestart


Private Sub Class_Initialize


FileName = ""


FilePath = ""


FileSize = 0


filestart= 0


FormName = ""


FileType = ""


End Sub





Public Function SaveAs (FullPath)


Dim Dr,errorchar,i


Saveas=true


' Response.Write FullPath &amp; ' .....................&lt;br&gt; '


' Filename= ' ss.txt '


If trim (FullPath) = "" or filestart=0 or filename= "" or right (fullpath,1) = "/" Then Exit Function


' Response.Write ' 2........................&lt;br&gt; '


Set dr=createobject ("ADODB.stream")


Dr. Mode=3


Dr. Type=1


Dr. Open


Sundyupload_sourcedata.position=filestart


Sundyupload_sourcedata.copyto dr,filesize


Dr. SaveToFile fullpath,2


Dr. Close


Set dr=nothing


Saveas=false


End Function


End Class

Class Progress
Dim Objdom,xmlpath
Dim StartTime
Private Sub Class_Initialize

End Sub

Public Sub Progressinit (xmlpathtmp)
Dim Objroot,objchild
Dim OBJPI

Xmlpath = xmlpathtmp


Set objdom = Server.CreateObject ("Microsoft.XMLDOM")


Set objroot = objdom.createelement ("Progress")


Objdom.appendchild Objroot





Set objchild = objdom.createelement ("TotalBytes")


Objchild.text = "0"


Objroot.appendchild Objchild


Set objchild = objdom.createelement ("Uploadbytes")


Objchild.text = "0"


Objroot.appendchild Objchild


Set objchild = objdom.createelement ("Uploadpercent")


Objchild.text = "0%"


Objroot.appendchild Objchild


Set objchild = objdom.createelement ("Uploadspeed")


Objchild.text = "0"


Objroot.appendchild Objchild


Set objchild = objdom.createelement ("TotalTime")


Objchild.text = "00:00:00"


Objroot.appendchild Objchild


Set objchild = objdom.createelement ("Lefttime")


Objchild.text = "00:00:00"


Objroot.appendchild Objchild





Set OBJPI = objdom.createprocessinginstruction ("xml", "version= ' 1.0 ' encoding= ' utf-8 '")


Objdom.insertbefore Objpi, objdom.childnodes (0)


Objdom.save Xmlpath


Set OBJPI = Nothing


Set Objchild = Nothing


Set Objroot = Nothing


Set objdom = Nothing


End Sub





Sub UpdateProgress (tbytes,rbytes)


Dim etime,currenttime,speed,totaltime,lefttime,percent


If rbytes = 0 Then


StartTime = Timer


Set objdom = Server.CreateObject ("Microsoft.XMLDOM")


Objdom.load (Xmlpath)


Objdom.selectsinglenode ("//totalbytes"). Text=tbytes


Objdom.save (Xmlpath)


Else


Speed = 0.0001


CurrentTime = Timer


ETime = Currenttime-starttime


If etime&gt;0 Then speed = Rbytes/etime


TotalTime = Tbytes/speed


Lefttime = (tbytes-rbytes)/speed


Percent = Round (rbytes *100/tbytes)


' Objdom.selectsinglenode ("//uploadbytes"). Text = Rbytes


' Objdom.selectsinglenode ("//uploadspeed"). Text = Speed


' Objdom.selectsinglenode ("//totaltime"). Text = TotalTime


' Objdom.selectsinglenode ("//lefttime"). Text = Lefttime


Objdom.selectsinglenode ("//uploadbytes"). Text = Formatfilesize (rbytes) &amp; "/" &amp; Formatfilesize (Tbytes)


Objdom.selectsinglenode ("//uploadpercent"). Text = percent


Objdom.selectsinglenode ("//uploadspeed"). Text = Formatfilesize (speed) &amp; "/sec"


Objdom.selectsinglenode ("//totaltime"). Text = Sectotime (totaltime)


Objdom.selectsinglenode ("//lefttime"). Text = Sectotime (lefttime)


Objdom.save (Xmlpath)


End If


End Sub

Private Function Sectotime (sec)


Dim h:h = "0"


Dim m:m = "0"


Dim s:s = "0"


H = Round (sec/3600)


M = Round (sec mod 3600)/60)


s = Round (sec mod 60)


If LEN (h) =1 Then h = "0" &amp; H


If LEN (m) =1 Then m = "0" &amp; M


If LEN (s) =1 Then s = "0" &amp; S


Sectotime = (H &amp; ":" &amp; M &amp; ":" &amp; S)


End Function





Private Function formatfilesize (fsize)


Dim radio,k,m,g,unittmp


K = 1024


m = 1024*1024


g = 1024*1024*1024


Radio = 1


If Fix (fsize/g) &gt; 0.0 Then


unittmp = "GB"


Radio = g


ElseIf Fix (fsize/m) &gt; 0 Then


unittmp = "MB"


Radio = M


ElseIf Fix (fsize/k) &gt; 0 Then


unittmp = "KB"


Radio = k


Else


unittmp = "B"


Radio = 1


End If


If Radio = 1 Then


Formatfilesize = fsize &amp; "&amp;nbsp;" &amp; unittmp


Else


Formatfilesize = FormatNumber (fsize/radio,3) &amp; Unittmp


End If


End Function

Private Sub Class_Terminate
Set objdom = Nothing
End Sub
End Class
%>
<script language= "JavaScript Tutorial" >
Start progress bar
function Startprogress (Xmlpath) {
Displayprogress ();
Setprogressdivpos ();
SetTimeout ("Displayprogressbar (' + Xmlpath +") ", 500);
}

function Displayprogressbar (Xmlpath) {


var xmldoc = new ActiveXObject ("msxml2.domdocument.3.0");


Xmldoc.async = false;


Xmldoc.load (Xmlpath);


if (xmldoc.parseerror.errorcode!=0) {


var error = Xmldoc.parseerror;


Alert (Error.reason)


SetTimeout ("Displayprogressbar (' + Xmlpath +") ", 1000);


Return


}


var root = xmldoc.documentelement; Root node


var totalbytes = root.childnodes (0). text;


var uploadbytes = root.childnodes (1). text;


var percent = Root.childnodes (2). text;


progresspercent.innerhtml = percent + "%";


ProgressBar.style.width = percent + "%";


uploadsize.innerhtml = uploadbytes;


uploadspeed.innerhtml = Root.childnodes (3). text;


totaltime.innerhtml = Root.childnodes (4). text;


lefttime.innerhtml = Root.childnodes (5). text;


if (percent&lt;100) {


SetTimeout ("Displayprogressbar (' + Xmlpath +") ", 1000);


}


}

function displayprogress () {


var objprogress = document.getElementById ("Progress");


ObjProgress.style.display = "";


}


function closeprogress () {


var objprogress = document.getElementById ("Progress");


ObjProgress.style.display = "None";


}


function Setprogressdivpos () {


var objprogress = document.getElementById ("Progress");


ObjProgress.style.top = document.body.scrolltop+ (Document.body.clientheight-document.getelementbyid ("Progress"). offsetheight)/2


ObjProgress.style.left = document.body.scrollleft+ (Document.body.clientwidth-document.getelementbyid ("Progress") . offsetwidth)/2;


}


&lt;/script&gt;


&lt;style type= "Text/css Tutorial" &gt;


. Progress {


Position:absolute;


padding:4px;


top:50;


left:400;


Font-family:verdana, Helvetica, Arial, Sans-serif;


font-size:12px;


width:250px;


height:100px;


Background: #FFFBD1;


Color: #3D2C05;


border:1px solid #715208;


* Mozilla Proprietary * *


-moz-border-radius:5px;


/*-moz-opacity:0.95; */


}


. Progress Table,.progress td{


font-size:9pt;


}

. bar{
width:100%;
height:15px;
Background-color: #CCCCCC;
border:1px inset #666666;
margin-bottom:4px;
}

. progresspercent{


font-size:9pt;


Color: #000000;


height:15px;


Position:absolute;


z-index:20;


width:100%;


Text-align:center;


}


. progressbar{


Background-color: #91D65C;


width:1px;


height:15px;


}


&lt;/style&gt;


&lt;div id= "Progress" style= "Display:none" class= "Progress" &gt;


&lt;div class= "Bar" &gt;


&lt;div id= "progresspercent" class= "Progresspercent" &gt;0%&lt;/div&gt;


&lt;div id= "ProgressBar" class= "ProgressBar" &gt;&lt;/div&gt;


&lt;/div&gt;


&lt;table border= "0" cellspacing= "0" cellpadding= "2" &gt;


&lt;tr&gt;


&lt;td&gt; already uploaded &lt;/td&gt;


&lt;td&gt;:&lt;/td&gt;


&LT;TD id= "Uploadsize" &gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt; Upload Speed &lt;/td&gt;


&lt;td&gt;:&lt;/td&gt;


&LT;TD id= "Uploadspeed" &gt;&amp;nbsp;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt; Total required Time &lt;/td&gt;


&lt;td&gt;:&lt;/td&gt;


&LT;TD id= "TotalTime" &gt;&amp;nbsp;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&lt;td&gt; remaining Time &lt;/td&gt;


&lt;td&gt;:&lt;/td&gt;


&LT;TD id= "Lefttime" &gt;&amp;nbsp;&lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;


&lt;/div&gt;

&lt;% @LANGUAGE = "VBSCRIPT" codepage= "65001"%&gt;


&lt;!--#include file= "sundyupload.asp"--&gt;


&lt;%


' This example document encoding is UTF-8, if it is another encoded system, please convert the encoding to the appropriate encoding, otherwise the form gets the data may be garbled


Dim objupload,opt


Dim Xmlpath


Dim Fileformname,objfile,counter


opt = Request. QueryString ("opt")


If opt = "Upload" Then


Xmlpath = Server.MapPath (Request. QueryString ("Xmlpath")) ' converts a virtual path to a real path


Set objupload=new sundyupload ' Build upload Object


Objupload.uploadinit Xmlpath, "Utf-8"


Counter = 1


Response.Write ("plain form:" &amp; Objupload.form ("Normalform") &amp; "&lt;BR&gt;&lt;BR&gt;") ' Get form data


For each fileformname in Objupload.objfile


Set Objfile=objupload.objfile (Fileformname)


FileSize = Objfile.filesize


strtemp= Objfile.filepath


Response.Write strtemp


FileName = Mid (Strtemp,instrrev (strtemp, "") +1)


' G0=


F0=replace (replace (now (), ":", ""), "", ""), "-", "" "&amp;". " &amp;g0





If fileSize &gt; 0 Then


Response.Write ("File Size:" &amp; fileSize &amp; "&lt;BR&gt;")


Response.Write ("File Name:" &amp; Objfile.filepath &amp; "&lt;BR&gt;")

T=split (FileName, ".")


T1=t (1)


Filename=replace (replace (now, ":", ""), "", ""), "-", "" "&amp;". " &amp;t1





' Response.Write ("File Description:" &amp; Objupload.form ("Filedesc" &amp; Counter) &amp; "&lt;BR&gt;&lt;BR&gt;")


Objfile.saveas Server.MapPath (".") &amp; "Upload" &amp; FileName


Response.Write "Save at:" &amp;server.mappath (".") &amp; "Upload" &amp; FileName &amp; "&lt;br&gt;&lt;br&gt;"


End If


Counter = counter + 1


Next





End If


' To upload a progress bar data file (the XML file specifies a virtual path)


' It's best to be random because it's possible for multiple people to upload at the same time and need different progress data


' This path needs to be passed in to the upload component at the time of submission to change the progress data during the upload process


' Client uses JavaScript to read this XML file, showing progress


Xmlpath = "upload/" &amp; Timer &amp; ". xml"


%&gt;


&lt;html&gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/&gt;


&lt;title&gt;sundy Upload Progress Bar example&lt;/title&gt;


&lt;script language= "JavaScript" &gt;


function Chkfrm () {


var objfrm = document.frmupload;


if (objfrm.file1.value== "" &amp;&amp; objfrm.file2.value== "") {


Alert ("Please select a file");


ObjFrm.file1.focus ();


return false;


}


Objfrm.action = "example.asp?opt=upload&amp;xmlpath=&lt;%=xmlpath%&gt;";


Startprogress (' &lt;%=xmlPath%&gt; ');//Start Progress bar


return true;


}


&lt;/script&gt;


&lt;/head&gt;

<body>
<form name= "Frmupload" method= "post" action= "example.asp" enctype= "Multipart/form-data" Onsubmit= "return chkfrm ()"
Plain form: <br><input type= "text" name= "Normalform" size= "><BR>" <br>
File 1:<br>
<input type= "file" Name= "File1" size= "></br>";
<input type= " Text "Name=" FILEDESC1 "size=" ><BR><BR>
file 2:<br>
<input type= "file" Name= "File2" Size= "></br>"
<input type= "text" name= "FILEDESC2" size= "><br>";
file 3:<br>
<input type= "File" Name= "File3" size= "></br>";
file 4:<br>
<input type= "file" Name= "File4" Size= "></br>"
file 5:<br>
<input type= "file" Name= "File5" size= "n" ></BR>
< Input type= "Submit" Name= "Btnsubmit" value= "submit"/>
</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.