Uploading Files with ASP
Please use the “Print” function at the bottom of the page to create a PDF.
For Windows Web Hosting packages
You can upload files using the browser using ASP. The following sample scripts show you how to do this:
ASP
MaxLength = x specifies the maximum file size in bytes
<head>
<title>Upload Files via ASP</title>
</head>
<body bgcolor="white">
<%=Font%>
<%
' --- ASP FileUpload Modul GetFILE (C) 1999 by Stefan Falz
' --- Active error handling
' --- On Error Resume Next
' --- Assign the error source that is to be output if an error occurs
Err.Source = "GetFILE HTTP-Upload"
' --- Constant for error heading
Const ErrHeader = "<b>ERROR</b><br><br>"
' --- Array declaration
Dim ErrArray(4)
ErrArray(0) = "10900 - File size exceeds maximum."
ErrArray(1) = "10901 - Unknown error.<br>"
ErrArray(2) = "10902 - No file has been sent or the transfer is faulty.<br>"
ErrArray(3) = "10903 - No text file has been sent.<br>"
' -- Calling the GetFILE Subroutine
Call GetFILE()
Private Sub GetFile()
' --- The HTTP stream is read in this subroutine.
' --- Declare the variables
Dim FileText
FileText = Request.BinaryRead(Request.TotalBytes)
Dim FileTextByte
FileTextByte = ""
Dim FileTextNew
FileTextNew = ""
Dim FilePosFirst
FilePosFirst = 0
Dim FilePosLast
FilePosLast = 0
Dim FileType
FileType = ""
Dim strRevText
strRevText = ""
Dim strFileName
strFileName = ""
Dim strFileNameOnly
strFileName = ""
Dim posFileName
posFileName = 0
Dim rghFile
rghFileName = ""
Dim lenFileTextByte
lenFileTextByte = 0
' --- Specify the max file size +500 bytes for file information
Dim maxLength
maxLength = 25500
' --- Query the size of the sent streams
If Request.TotalBytes > maxLength Then
Call Error_Handler(10900)
Exit Sub
End if
' --- Search for HexCode 0D 0A 0D 0A (Beginning of file within HTTP stream)
FilePosFirst = InStrB(FileText, (ChrB(13) & ChrB(10) & ChrB(13) & ChrB(10)))
' --- Search for HexCode 0D 0A 2D 2D (End of file within the HTTP stream)
FilePosLast = InStrB(FileText, (ChrB(13) & ChrB(10) & ChrB(45) & ChrB(45)))
' --- Search for HexCode 2D 0A 0D within the reversed HTTP stream,
' --- since there may be multiple end-of-file identifiers.
strRevText = StrReverse(FileText)
FilePosLast = InStrB(strRevText, (ChrB(45) & ChrB(10)) & ChrB(13))
FilePosLast = LenB(FileText) - FilePosLast
' --- Abort if file size is 0 bytes
If FilePosFirst = 0 Or FilePosLast = 0 Or FilePosLast - FilePosFirst < 5 Then
Call Error_Handler(10902)
Exit Sub
End If
' --- Abort if "Content-Disposition" is no specified
If InStrB(FileText, ChrB(67) & ChrB(111) & ChrB(110) & ChrB(116) & ChrB(101) & ChrB(110) & ChrB(116) & ChrB(45) & ChrB(84) & ChrB(121) & ChrB(112) & ChrB(101) & ChrB(58) & ChrB(32) & ChrB(116) & ChrB(101) & ChrB(120) & ChrB(116) & ChrB(47)) = 0 Then
Call Error_Handler(10903)
Exit Sub
End if
' --- Abort if an error has occurred that is not one of the errors listed above.
If Err.Number <> 0 Then
Call Error_Handler(10901)
Exit Sub
End if
' --- Determination of the file name incl. path within the HTTP stream
posFileName = InStrB(FileText, ChrB(102) & ChrB(105) & ChrB(108) & ChrB(101) & ChrB(110) & ChrB(97) & ChrB(109) & ChrB(101) & ChrB(61) & ChrB(34)) + 9
rghFileText = RightB(FileText, LenB(FileText) - posFileName)
strFileName = LeftB(rghFileText, InStrB(rghFileText, ChrB(34)) - 1)
Response.Write "<strong>File name incl. path: </strong>"
Response.BinaryWrite strFileName
Response.Write "<br>"
' --- Determines the file name excl. path within the HTTP stream.
posLastSlash = InStrB(StrReverse(strFileName), ChrB(92))
strFileNameOnly = MidB(strFileName, LenB(strFileName) - posLastSlash, posLastSlash + 1)
Response.Write "<strong>File name excl. path: </strong>"
Response.BinaryWrite strFileNameOnly
Response.Write "<br>"
' --- Abort if error occurs
If Err <> 0 Then
Call Error_Handler(10901)
Exit Sub
End if
' --- Writing the file contents in ByteArray
FileTextByte = MidB(FileText, (FilePosFirst + 4), (FilePosLast - (FilePosFirst + 4)))
' --- Determining the file size by reading the length of the determined file contents
lenFileTextByte = LenB(FileTextByte)
' --- Conversion of the file content (binary stream) to ASCII characters
For i = 1 To lenFileTextByte
FileTextNew = FileTextNew & Chr(AscB(MidB(FileTextByte, i, 1)))
Next
' --- Write the determined file contents to a file.
Set objFileSys = Server.CreateObject("Scripting.FileSystemObject")
Set File = objFileSys.CreateTextFile(Server.MapPath("./") & "\" & Session.SessionID & ".tmp", True, False)
File.WriteLine CStr(FileTextNew)
File.Close
Set File = Nothing
Set objFileSys = Nothing
' --- Output of the file size in bytes
Response.Write "<strong>File size: </strong>" & lenFileTextByte & " bytes.<br><br>"
' --- Convert file content because HTML pages are not displayed correctly
FileTextNew = Replace(FileTextNew, "<", "<")
FileTextNew = Replace(FileTextNew, ">", ">")
FileTextNew = Replace(FileTextNew, VbCrLf, "<br>" & VbCrLf)
FileTextNew = Replace(FileTextNew, Chr(9), " ")
' --- Output of the file content
Response.Write FileTextNew
End Sub
Private Sub Error_Handler(intErrNumber)
' --- Determination of the transferred error code and output on the screen
Select Case intErrNumber
Case 10900: Response.Write ErrHeader & ErrArray(0)
Case 10901: Response.Write ErrHeader & ErrArray(1)
Case 10902: Response.Write ErrHeader & ErrArray(2)
Case 10903: Response.Write ErrHeader & ErrArray(3)
Case Else: Response.Write ErrHeader & Err.Description
End Select
Exit Sub
End Sub
%>
</body>
</html>
For example, a simple HTML form serves as user interface.
<html>
<head>
<title>Upload Files via ASP</title>
</head>
<body>
<form method="POST" action="upload.asp" enctype="multipart/form-data" target="_new">
<input type="file" name="File" size="50"><br>
<input type="submit" value="START FILE UPLOAD" name="Submit">
</form>
</body>
</html>
ASP.NET
With ASP.NET the upload script can be realized as follows:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Specify the path on the server to
' save the uploaded file to.
Dim savePath As String = "e:\Kunden\Homepages\11\d12345678\www\UploadTest"
' Before attempting to perform operations
' on the file, verify that the FileUpload
' control contains a file.
If (FileUpload1.HasFile) Then
' Get the name of the file to upload.
Dim fileName As String = FileUpload1.FileName
' Append the name of the file to upload to the path.
savePath += fileName
' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user of the name the file
' was saved under.
UploadStatusLabel.Text = "Your file was saved as " & fileName
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>File to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
Hint: In the following line you still have to enter the correct path to your web space:
Dim savePath As String = "e:\Kunden\Homepages\11\d12345678\www\UploadTest"