CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> Archive -> GotoCode Archive

 asp upload help

Print topic Send  topic

Author Message
asp upload
Posted: 08/19/2002, 2:59 PM

How to make form in asp that will help visitor to upload files from visitor hard drive to server??
Like some web page which have textbox, browse button and upload button??
Ken Hardwick
Posted: 08/19/2002, 4:33 PM

Copy and paste the following into a text editor and save as "upload.asp"
Then create a hyperlink to it. It will open into a new window and will prompt
user to close window after upload.



<%' Please enter password as "Admin" ----> case sensitive,

IntPass = "Admin"
FileDir = "C:\temp"

RedirName = Mid(Request.ServerVariables("Script_Name"),InstrRev(Request.ServerVariables("Script_Name"),"/") + 1)
Response.Buffer = True
Response.Clear
Dim name,Password,Login




Session("AccessD") = "1"
Session("FolderPrim") = left(Request.ServerVariables("PATH_TRANSLATED"),instrrev(Request.ServerVariables("PATH_TRANSLATED"),"\",len(Request.ServerVariables("PATH_TRANSLATED"))))
Session("Inter") = left(Request.ServerVariables("PATH_TRANSLATED"),instrrev(Request.ServerVariables("PATH_TRANSLATED"),"\",len(Request.ServerVariables("PATH_TRANSLATED"))))
Session("ChkSpace") = "1"
Session("NumFol") = 0
Session("Open") = "True"



If Not Session("Open") = "True" then%>
<title>Muti File Upload</title>
<BODY bgcolor=#3366CC onload="window.Introd.PassW.focus();">
<FORM name="Introd" method="post" action="<%=RedirName%>">
<table align=left border="1" bordercolor="#000000" cellspacing="0" cellpadding="0" width=250 bgcolor="#800000" style="border-collapse: collapse">
<tr><td align="center">
<P><font class=normal color="#FFFFFF">Password:</font> <input type=password name=PassW ID=PassW size=8> <input type=submit class=boton2 name=Entrar ID=Entrar value=" Enter "></P>
</td></tr></table></form><br><br><br>
<p align=left style="font-family:verdana,helvetica,arial,sans serif;color:#000000;font-size:8pt;font-weight:bold;"><%= date%></p>
</BODY></HTML>
<% Session("Open") = "True"
Response.End
else
if Session("AccessD") = "1" then
else
Login = "True"
Password = Request.Form("PassW")
Password = intpass
if not Password = IntPass then Login = "False"
If Login = "False" then
Session.Abandon
Response.Redirect RedirName
Response.End
else
Session("AccessD") = "1"
Session("FolderPrim") = left(Request.ServerVariables("PATH_TRANSLATED"),instrrev(Request.ServerVariables("PATH_TRANSLATED"),"\",len(Request.ServerVariables("PATH_TRANSLATED"))))
Session("Inter") = left(Request.ServerVariables("PATH_TRANSLATED"),instrrev(Request.ServerVariables("PATH_TRANSLATED"),"\",len(Request.ServerVariables("PATH_TRANSLATED"))))
Session("ChkSpace") = "1"
Session("NumFol") = 0
end if
end if
End If
Response.write "<b>Please click on the browse button to select your Crystal Report, then click on the upload button.</b><br><br><br>"

'-----------------------------------------------------------------------------
'Muli File's upload Created by Bhushan Paranjpe
'-----------------------------------------------------------------------------
Sub BuildUploadRequest(RequestBin)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
if PosEnd = 0 then
Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
Response.Write "Please correct the form attributes and try again."
Response.End
end if
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries
Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
'Members variable of objects are put in a dictionary object
Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary")
'Get an object name
Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
Pos = InstrB(Pos,RequestBin,getByteString("name="))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
PosBound = InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 AND (PosFile<PosBound) Then
'Get Filename, content-type and content of file
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
FileName = Mid(FileName,InStrRev(FileName,"\")+1)
'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = FileName
ValueBeg = PosBeg-1
ValueLen = PosEnd-Posbeg
Else
'Get content of object
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
ValueBeg = 0
ValueEnd = 0
End If
'Add content to dictionary object
UploadControl.Add "Value" , Value
UploadControl.Add "ValueBeg" , ValueBeg
UploadControl.Add "ValueLen" , ValueLen
'Add dictionary object to main dictionary
UploadRequest.Add name, UploadControl
'Loop to next object
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
End Sub

'String to byte string conversion
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
char = Mid(StringStr,i,1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function

'Byte string to string conversion
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

Function UploadFormRequest(name)
on error resume next
if UploadRequest.Item(name) then
UploadFormRequest = UploadRequest.Item(name).Item("Value")
end if
End Function

'Process the upload
UploadQueryString = Replace(Request.QueryString,"AF_upload=true","")
if mid(UploadQueryString,1,1) = "&" then
UploadQueryString = Mid(UploadQueryString,2)
end if

AF_uploadAction = CStr(Request.ServerVariables("URL")) & "?AF_upload=true"
If (Request.QueryString <> "") Then
if UploadQueryString <> "" then
AF_uploadAction = AF_uploadAction & "&" & UploadQueryString
end if
End If

If (CStr(Request.QueryString("AF_upload")) <> "") Then

response.write "<b><font size = 12><a href=""JavaScript:parent.window.close()"">File Uploaded, click here to close window.</a></b></font>"

F_redirectPage = "upload.asp"

If (AF_redirectPage = "") Then




F_redirectPage = CStr(Request.ServerVariables("URL"))

end if

RequestBin = Request.BinaryRead(Request.TotalBytes)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin

AF_keys = UploadRequest.Keys
for AF_i = 0 to UploadRequest.Count - 1
AF_curKey = AF_keys(AF_i)
'Save all uploaded files
if UploadRequest.Item(AF_curKey).Item("FileName") <> "" then
AF_value = UploadRequest.Item(AF_curKey).Item("Value")
AF_valueBeg = UploadRequest.Item(AF_curKey).Item("ValueBeg")
AF_valueLen = UploadRequest.Item(AF_curKey).Item("ValueLen")

if AF_valueLen = 0 then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
Response.Write "File does not exists or is empty.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
response.End
end if

'Create a Stream instance
Dim AF_strm1, AF_strm2
Set AF_strm1 = Server.CreateObject("ADODB.Stream")
Set AF_strm2 = Server.CreateObject("ADODB.Stream")

'Open the stream
AF_strm1.Open
AF_strm1.Type = 1 'Binary
AF_strm2.Open
AF_strm2.Type = 1 'Binary

AF_strm1.Write RequestBin
AF_strm1.Position = AF_ValueBeg
AF_strm1.CopyTo AF_strm2,AF_ValueLen

'Create and Write to a File
AF_curPath = Request.ServerVariables("PATH_INFO")
AF_curPath = Trim(Mid(AF_curPath,1,InStrRev(AF_curPath,"/")) & "")
if Mid(AF_curPath,Len(AF_curPath),1) <> "/" then
AF_curPath = AF_curPath & "/"
end if
on error resume next



AF_strm2.SaveToFile FileDir & "\" & UploadRequest.Item(AF_curKey).Item("FileName"),2
if err then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
err.clear
response.End
end if
end if
next


If (AF_redirectPage <> "") Then
If (InStr(1, AF_redirectPage, "?", vbTextCompare) = 0 And UploadQueryString <> "") Then
AF_redirectPage = AF_redirectPage & "?" & UploadQueryString
End If
Call Response.Redirect(AF_redirectPage)
end if

end if
if UploadQueryString <> "" then
UploadQueryString = UploadQueryString & "&AF_upload=true"
else
UploadQueryString = "AF_upload=true"
end if

%>
<html>
<head>
<title>Muti File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
<!--

function getFileExtension(filePath) { //v1.0
fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\')+1,filePath.length));
return fileName.substring(fileName.lastIndexOf('.')+1,fileName.length);
}

function checkFileUpload(form,extensions) { //v1.0
document.MM_returnValue = true;
if (extensions && extensions != '') {
for (var i = 0; i<form.elements.length; i++) {
field = form.elements;
if (field.type.toUpperCase() != 'FILE') continue;
if (field.value == '') {
alert('File is required!');
document.MM_returnValue = false;field.focus();break;
}
if (extensions.toUpperCase().indexOf(getFileExtension(field.value).toUpperCase()) == -1) {
alert('This file is not allowed for uploading!');
document.MM_returnValue = false;field.focus();break;
} } }
}
//-->
</script>
</head>
<body bgcolor="#3366CC" text="#000000">
<table width="450" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<form name="ASP" method="post" action="<%=AF_uploadAction%>" enctype="multipart/form-data" onSubmit="checkFileUpload(this,'');return document.MM_returnValue">
<div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#000000"> </font><input type="file" name="Files" size="20"> 


<p><input type="submit" name="Submit1" value="upload"></div>
</form>
</td>
</tr>
</table>
</body>
</html>

   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.