This tutorial will show you the need to build a simple application that allows users to upload PDF files to the server steps, delete existing files and view the files in their browsers. This example uses our own dgfileupload components and you can download a 15-day trial version here where you can follow the tutorials and test your own server to complete the application. All the source code, along with the small Access 2000 database tutorial used in this tutorial, can be downloaded here as a zip file.
After downloading and canceling the Stile Archive, create your site root directory (such as ' c:inetpub wwwroot dgupload '), and put two ASP pages (new directory ' upload.asp tutorials ' and ' display.asp ') and the database there. You must also create a "PDF" and set the security for this directory so that the users of the ' fully-controlled Internet Guest account '. To do this, right-click on the ' PDF file folder and select ' Properties ' and then ' secure ' tab. If the ' Internet Guest account ' is not already listed, click the Add button, and then select the computer name from the ' Find ' drop-down menu. Scroll down until you see ' for IUSR_ [computer name] ', select this user and click ' Add ' button. Click ' OK ' to return to the ' Properties ' window, select the newly added user and give the item ' Full Control '. You can now test your application into the browser by entering the following URL:
Http://localhost/dgUpload/upload.asp
On this page you will see two input fields, first, you can select the PDF file you want to upload and other display names that let you enter the file. This name will be displayed as a link to view the ' display.asp file ' after uploading. Browse to a suitable PDF file, enter a name, and click the ' Upload ' button. Your file will be uploaded and you will be redirected to the display page, which will have 1 links to the file. Details (path, display name and date upload per file) are stored in the database. Let's take a look at the code to execute this.
We're declaring variables, and the first thing we have to do is open a connection to the database.
Set cnndb = Server.CreateObject ("ADODB. Connection ")
strconn = "Driver={microsoft Access DRIVER (*.mdb)};" & _
"Dbq=" & Server.MapPath ("Newsletters.mdb")
Cnndb.open strconn
Set opost = Server.CreateObject ("Dgfileupload.dgupload")
Because we want to restrict the file size of the uploaded PDFs to something reasonable we are set to megabytes.
Opost.maxfilesize = 10000000
Next We grab the data posted from the form by calling to the ' Getpostdata ' method.
Opost.getpostdata ()
The following code grabs the file name and display name from the form collection, establishes and executes a SQL query to store the file details in the database, and then saves the file to the directory specified on the server. See ' source upload.asp ' to see if this code is integrated with the rest of the application.
Get the filename and display name
Strdisplayname = Opost.form ("DisplayText")
strFileName = Opost.files ("Pdffile"). FileName
' Build the INSERT query
strquery = "INSERT into newsletters (Filename, DisplayName) VALUES ('" & strFileName & "', '" & Strdisplayname & "')"
Cnndb.execute (strquery)
' Get "folder where we want to store the PDF
Strfolder = Server.MapPath ("PDFs") & ""
' Save the uploaded PDF to the specified directory
Intresult = Opost.files ("Pdffile"). Save (Strfolder & strFileName, 1)
' If the file upload is successful, redirect to
' The page that displays the files ("display.asp")
If intresult = 0 Then
Cnndb.close
Set cnndb = Nothing
Response.Redirect ("display.asp")
Else
Strfeedback = "File upload failed. Error # "& Intresult
End If
If there is a problem or the ' strfeedback ' variable in the upload save process is assigned a suitable message and then displayed to the user. This is handled using client JavaScript once the page has been processed. Again, see how the source code provides more details on how to work.
As mentioned earlier, the above example uses dgfileupload but this process is used by any similar file upload component. The remainder of this tutorial is to extract a file from the Michael Allensmith written upload component of the article, describing the upload file using the other two components of the process, the software and the persistence of aspupload craftsman fileup.
The two (Fileup and aspupload) processing uploads are slightly different. Let's code for these components to handle each of our requirements (upload only below the specified size image file).
The form of uploading a file on the client, is the same two.
Form name= "form" action= "upload.asp" enctype= "Multipart/form-data" method= "POST" >
<input type= "File" name= "File1" >
<input type= "Submit" value= "Upload Files" >
</form>
<%
Intmaxfilesize = 8000
Struploadfolder = "C:uploadfolder"
Function Isfilesizeok (bytes)
' Restrict file byte size
Bytemax = Intmaxfilesize
If bytes > Bytemax Then
Isfilesizeok = FALSE
Else
Isfilesizeok = TRUE
End If
End Function
Function isvalidfile (filename)
' Define what file types you'll permit to upload
FileExtension = LCase (Right (filename,4))
Select Case FileExtension
Case ". gif", ". jpg", ". png", "JPEG"
Isvalidfile = TRUE
Case Else
Isvalidfile = FALSE
End Select
End Function
%>
Now the code for Software artisans Fileup.
<%
Sub uploadsa
Set up = Server.CreateObject ("Softartisans.fileup")
up. Path = Uploadfolder
If not up. IsEmpty Then
filename = Mid (up. Userfilename, InStrRev (up. Userfilename, "") + 1)
' restrict file types to upload
If isvalidfile (filename) Then
' Restrict file by Size
If isfilesizeok (up. totalbytes) Then
up. Save
strUploadStatus1 = "File [" & filename & "] uploaded successfully!" & up. TotalBytes
Else
strUploadStatus1 = "Error:file Too Large:" & filename & "(" & amp; up. TotalBytes & "bytes)"
end If
Else
STRUPLOADSTATUS1 = "Error:this File Type is restricted f Rom uploading: " & filename
End If
End If
Set up = no
end Sub
%>
The
insists on AspUpload, you save the first file, and then perform the check. If the file does not pass the check, then the code deletes it from the server. The component also has the "savetomemory" option, which bypasses writing to disk until it is indicated. AspUpload has built-in image processing and size can detect a file is a with. ImageType the image of the property, but in this example we will use the Isvalidfile feature.
<%
Sub uploadpersists
Set up = Server.CreateObject ("Persits.upload.1")
up. Overwritefiles = TRUE
up. Setmaxsize intmaxfilesize
up. Save Uploadfolder
For each File in the up. Files
FileName = file.extractfilename
If isvalidfile (fileName) Then
if Isfil Esizeok (file.originalsize) Then
strUploadStatus2 = "File [" & filename & "] uploaded succes sfully! "
Else
strUploadStatus2 =" Error:file Too Large: "& FileName &" ("& ; file.originalsize & "bytes"
file.delete
End If
Else
& nbsp File.delete
STRUPLOADSTATUS2 = "Error:this File Type is restricted from uploading:" & Filenam E
End If
Next
End Sub
%>