Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Uploading file from asp to web server
Message
From
15/10/2002 15:41:34
Fabian Belo
Independent Developer
Argentina
 
 
To
15/10/2002 14:14:44
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00711412
Message ID:
00711470
Views:
10
You'll need two pages. An HTML (files_upload1.htm) and an ASP (file_upload.asp) as folowing:

files_upload1.htm
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>File Upload</title>
</head>

<body bgcolor="#000000" text="#FFCC00" link="#FFFF00" vlink="#FFFF00" alink="#FFFF00">

<form method="POST" enctype="multipart/form-data" action="files_upload.asp">
  <p><img border="0" src="files_logo.jpg" width="251" height="74"></p>
  <p> </p>
  <div align="center">
    <center>
    <table border="0" style="border-collapse: collapse" bordercolor="#111111" width="74%" id="AutoNumber1">
      <tr>
        <td width="100%" colspan="2" bgcolor="#C0C0C0" style="color: #000000"><b><font face="Verdana" size="3" color="#000000"> 
        File Upload</font></b></td>
      </tr>
      <tr>
        <td width="50%"><b><font size="2" face="Verdana" color="#FFCC00">File</font></b></td>
        <td width="50%"><font face="Verdana" size="2" color="#FFCC00"><b>
        <input type="file" name="File1" size="20"></b></font></td>
      </tr>
      <tr>
        <td width="100%" colspan="2"><b>
        <font face="Verdana" size="2" color="#FFCC00">
        <input type="submit" value="Submit" name="B1"></font></b></td>
      </tr>
    </table>
    </center>
  </div>
</form>

<p align="center"><b><font face="Verdana" size="2" color="#FFCC00"><a href="files_ppal.asp">Back</a></font></b></p>

</body>

</html>
files_upload.asp
<%@LANGUAGE="VBSCRIPT"%>
<%response.buffer=true
Func = Request("Func")
if isempty(Func) Then
Func = 1
End if
Select Case Func
Case 1
%>

<head>
<title></title>
</head>

<body bgcolor="#000000" text="#FFCC00" vlink="#0000FF" alink="#0000FF">

<p><img border="0" src="files_logo.jpg"></p>

<table width="360" border="0" align="center">
<tr>
<td> 
<div align="center"><font face="Verdana" size="3" color="#FFFFFF"><b>Select file</b></font> </div>
</td>
</tr>
</table>
<FORM ENCTYPE="multipart/form-data" ACTION="file_upload.asp?func=2" METHOD=POST id=form1 name=form1> 
<TABLE align="center">
<TR> 
<TD><font face="Verdana" size="2" color="#FFCC00"> </font></TD>
</TR>
<TR> 
<TD><font color="#FFCC00" size="2" face="Verdana">Click Browse and select file to Upload<BR>
<BR>
</font></TD>
</TR>
<TR> 
<TD><font color="#FFCC00" size="2" face="Verdana">Then click on Upload<BR>
<BR>
</font></TD>
</TR>
<TR> 
<TD><STRONG><font color="#FFCC00" size="2" face="Verdana">File Name</font></STRONG></TD>
</TR>
<TR> 
<TD> <font size="2" face="Verdana" color="#FFCC00"> 
<INPUT NAME=File1 SIZE=30 TYPE=file>
<BR>
</font></TD>
</TR>
<TR> 
<TD align=left> 
<font face="Verdana" size="2" color="#FFCC00"> 
<INPUT type="submit" value="Upload">
<BR>
<BR>
</font>
</TD>
</TR>
<TR> 
<TD><font face="Verdana" size="2" color="#00FF00">Wait, you will be notified when process is complete<BR>
<BR>
  </font></TD>
</TR>
</TABLE>
<%
Case 2
ForWriting = 2
adLongVarChar = 201
lngNumberUploaded = 0

'Get binary data from form 
noBytes = Request.TotalBytes 
binData = Request.BinaryRead (noBytes)
'convery the binary data to a string
Set RST = CreateObject("ADODB.Recordset")
LenBinary = LenB(binData)

if LenBinary > 0 Then
RST.Fields.Append "myBinary", adLongVarChar, LenBinary
RST.Open
RST.AddNew
RST("myBinary").AppendChunk BinData
RST.Update
strDataWhole = RST("myBinary")
End if
'Creates a raw data file for with all da
' ta sent. Uncomment for debuging. 
'Set fso = CreateObject("Scripting.FileSystemObject")
'Set f = fso.OpenTextFile(server.mappath(".") & "\raw.txt", ForWriting, True)
'f.Write strDataWhole
'set f = nothing
'set fso = nothing
'get the boundry indicator
strBoundry = Request.ServerVariables ("HTTP_CONTENT_TYPE")
lngBoundryPos = instr(1,strBoundry,"boundary=") + 8 
strBoundry = "--" & right(strBoundry,len(strBoundry)-lngBoundryPos)
'Get first file boundry positions.
lngCurrentBegin = instr(1,strDataWhole,strBoundry)
lngCurrentEnd = instr(lngCurrentBegin + 1,strDataWhole,strBoundry) - 1
Do While lngCurrentEnd > 0
'Get the data between current boundry an
' d remove it from the whole.
strData = mid(strDataWhole,lngCurrentBegin, lngCurrentEnd - lngCurrentBegin)
strDataWhole = replace(strDataWhole,strData,"")

'Get the full path of the current file.
lngBeginFileName = instr(1,strdata,"filename=") + 10
lngEndFileName = instr(lngBeginFileName,strData,chr(34)) 
'Make sure they selected at least one fi
' le. 
if lngBeginFileName = lngEndFileName and lngNumberUploaded = 0 Then

Response.Write "<H2> The following error has ocurred.</H2>"
Response.Write "You must select a file"
Response.Write "<BR><BR>Press Back and try again"
Response.Write "<BR><BR><INPUT type='button' onclick='history.go(-1)' value='<< Back' id='button'1 name='button'1>"
Response.End 
End if
'There could be one or more empty file b
' oxes. 
if lngBeginFileName <> lngEndFileName Then
strFilename = mid(strData,lngBeginFileName,lngEndFileName - lngBeginFileName)
'Creates a raw data file with data betwe
' en current boundrys. Uncomment for debug
' ing. 
'Set fso = CreateObject("Scripting.FileSystemObject")
'Set f = fso.OpenTextFile(server.mappath(".") & "\raw_" & lngNumberUploaded & ".txt", ForWriting, True)
'f.Write strData
'set f = nothing
'set fso = nothing

'Loose the path information and keep jus
' t the file name. 
tmpLng = instr(1,strFilename,"\")
Do While tmpLng > 0
PrevPos = tmpLng
tmpLng = instr(PrevPos + 1,strFilename,"\")
Loop

FileName = right(strFilename,len(strFileName) - PrevPos)

'Get the begining position of the file d
' ata sent.
'if the file type is registered with the
' browser then there will be a Content-Typ
' e
lngCT = instr(1,strData,"Content-Type:")

if lngCT > 0 Then
lngBeginPos = instr(lngCT,strData,chr(13) & chr(10)) + 4
Else
lngBeginPos = lngEndFileName
End if
'Get the ending position of the file dat
' a sent.
lngEndPos = len(strData) 

'Calculate the file size. 
lngDataLenth = lngEndPos - lngBeginPos
'Get the file data 
strFileData = mid(strData,lngBeginPos,lngDataLenth)
'Create the file. 
Set fso = CreateObject("Scripting.FileSystemObject")
'Set f = fso.OpenTextFile(server.mappath("rte\scripts\upload\")  &_
'FileName, ForWriting, True)




' DEFINE/CHANGE YOUR SERVER FOLDER 
Set f = fso.OpenTextFile(server.mapPath(".") & "\" & "uploads\" & FileName, 2, True)
f.Write strFileData
Set f = nothing
Set fso = nothing

lngNumberUploaded = lngNumberUploaded + 1

End if

'Get then next boundry postitions if any
' .
lngCurrentBegin = instr(1,strDataWhole,strBoundry)
lngCurrentEnd = instr(lngCurrentBegin + 1,strDataWhole,strBoundry) - 1
loop


Response.Write "File Uploaded"
Response.Write lngNumberUploaded & " File successfully uploaded<BR>"
Response.Write "<BR><BR><INPUT type='button' onclick='document.location=" & chr(34) & "file_upload.asp" & chr(34) & "' value='<< More files' id='button'1 name='button'1>" 
%>
<font face="Verdana" color="#FFFFFF" size="2">                </font>
<p><font face="Verdana" color="#FFFFFF" size="2">                                                                            
<a href="files_ppal.asp">Back</a></font>
<%End Select %>
</p>
</form>
</BODY>
Besides, you need the FileSystemObject enabled in your Webserver
That's all. Works great. No COMs, no ActiveX, pure ASP code.
Regards.


>Hello gays, my question if relative to resolve the problem of upload a file from and asp client page to a web server using the ftp services offered the windows api or any other way without use of components or activex.
>
>Thanks in advance for any help
"Since I've read that alcohol is bad... I quit reading."
Me


http://www.fabianbelo.com.ar
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform