Pure ASP File Upload

来源:百度文库 编辑:神马文学网 时间:2024/04/27 18:12:11
byJacob Gilley
As a regular to ASP 101, I feel it neccesary to give a little back to the site that keeps me on top of things in the world of ASP. And so, I give you the FileUploader ASP Library. In a nutshell, the FilerUploader library is simply a single ASP include that provides an object-oriented approach to getting files from Internet clients.
I have seen numerous "pure ASP" upload solutions, some you even had to purchase, that did the job, but required you to wade through pages of code just to see how things worked. Then there are the COM components that you can buy that do what you want and more, but cost an arm and leg to obtain. What I wanted was something simple, centralized, easy to use and FREE! The FileUploader Library was the solution and now I want to share my work with fellow developers.
NOTE - YOU MUST HAVE VBSCRIPT v5.0 INSTALLED ON YOUR WEB SERVER FOR THIS LIBRARY TO FUNCTION CORRECTLY. YOU CAN OBTAIN IT FREE FROM MICROSOFT WHEN YOU INSTALL INTERNET EXPLORER 5.0 OR LATER.
The library consists of two VBScript classes: FileUploader and UploadedFile. The FileUploader is the top-level object in the model and is the only one that you will need to instantiate.
Here is how to do it: (this assumes that you have included "upload.asp")
<%Dim MyUploaderSet MyUploader = New FileUploader%>
Now once your FileUploader object is initialized, you can begin recieving the uploaded data from the client by a single call to the Upload() method.
<%MyUploader.Upload()%>
Or, since Upload() is the default method, you can use a shortcut and start the upload process like this: (Use one or the other, not both)
<%MyUploader()%>
Once that is complete, you can begin access and saving the file to your hard-drive or to a database. The uploaded files are accessed through the Files Collection of the FileUploader object. The Files Collection is a set of UploadedFile objects that represent each file uploaded.
Here is an example of how to enumerate the Files Collection and write each uploaded files‘ information to the browser:
<%Dim FileFor Each File In MyUploader.Files.ItemsResponse.Write "File Name:" & File.FileNameResponse.Write "File Size:" & File.FileSizeResponse.Write "File Type:" & File.ContentTypeNext%>
Here is an example of how to access a specific file in the Files Collection using the HTML file input element name as the Index:
<%Response.Write "File Name:" & MyUploader.Files("file1").FileNameResponse.Write "File Size:" & MyUploader.Files("file1").FileSizeResponse.Write "File Type:" & MyUploader.Files("file1").ContentType%>
For simplicity sake, I will be the using the For Each...Next variation for the following samples. Now, all the files are uploaded and its time to put them somewhere. There are two places you can save the uploaded files...on your hard-drive or to a database.
Here is how to save the files to disk:
<%Dim FileFor Each File In MyUploader.Files.ItemsFile.SaveToDisk "C:\UploadedFiles"Next%>
And how to save to a database: (Replace "MyUploadTable", "CONNECT STRING.." and field names accordingly)
<%Dim RSDim FileSet RS = Server.CreateObject("ADODB.Recordset")RS.Open "MyUploadTable", "CONNECT STRING OR ADO.Connection", 2, 2For Each File In MyUploader.Files.ItemsRS.AddNewRS("filename") = File.FileNameRS("filesize") = File.FileSizeRS("contenttype") = File.ContentTypeFile.SaveToDatabase RS("filedata")RS.UpdateNextRS.Close%>
I think that should just about cover everything. I hope this helps out and that you found my blathering somewhat interesting. If you have any questions or comments about this article or the code provided, feel free to email me. (avis7@airmail.net)
You can download the library and sample fileshere (4 KB).
p34c3 +0 4|| c0d3rz!
Update: Pure ASP File Upload Gets a Speed Boost
Jacob Gilley‘s Pure ASP File Upload is an effective, free method for uploading user files to your server. Although well implemented, I found that the file I/O needed improvement. Concatenation is quite slow in VBScript and it was actually faster to write a single byte at a time to the disk than to concatenate the file prior to saving.
Here Robbert Nix‘simproved concatenation algorithm comes to the rescue. I stumbled upon this algorithm when searching for performance enhancements in VBScript. Essentially Nix‘s code allows Gilley‘s Pure ASP File Upload to return to concatenation as a viable method for preparing the file prior to saving.
Two files are included with this implementation: upload.asp and buffering.inc. These files are meant to be incorporated with the existing distribution for Gilley‘s Pure ASP File Upload. The upload.asp file replaces the existing version and the include file buffering.inc is used to improve upload time.
Note: if you replace your existing version of upload.asp you must also copy buffering.inc to the same directory! No changes in existing upload pages are required to take advantage of this upgrade.
Gilley‘s Pure ASP File Upload files are needed and can be found above.
You can download the updated upload.asp and new buffering.inc in a zip file from here:fileuploader_buffering.zip (3.4 KB)
--WillDo Many Things ... Wellwbic16@hotmail.comUpdate: Even Faster Yet...
It seems that the update above caught the atention of one of our other authors... here‘s his message:
Hello. I already have a few articles posted on your website so far. I just got your newsletter concerning ASP uploads: Update: Pure ASP File Upload Gets a Speed Boost
While this may be faster - it is still very slow. I have found a way around this in the past and had posted my findings on planet source code at:
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=7361&lngWId=4
I am including thezip file (28 KB) for you to go over and review with this message. The zip file also includes a screen shot to demonstrate the simplicity of the code, and also contains a working sample. It is commented well and uses classes for a more object-oriented approach. Rather then converting each character between ansi and unicode 1 at a time - this class calls the ADODB Stream object to do it all at once. ADO 2.5+ is required along with vbScript 5.0+
I have had very good reviews posted by others about this code on planet source code.
Allowing files to be translated quicker allows users to post larger files before the server times out. Happy Coding!
Lewis@Moten.com
http://www.lewismoten.com
Update: Listing the Files That Have Been Uploaded
Our "Pure ASP File Upload" article has been getting update after update recently and here‘s one more. When we left it last, updated files were getting entered into a database. This update brings a simple little script to display a list of those files. Here‘s the message:
I just wanted to share some code I came up with to display all the files uploaded to the database using Lewis Moten‘s code (which was very useful and fairly easy to use). I needed to create an area on our company website where the advertising and sales staff could share files back and forth easily but I found that Lewis‘s code only took care of the upload part. We needed to be able to view all the files as well.
Well, here it is. It just runs through the database until it reaches End Of File (EOF) and displays the filenames as links to that file. It requires Lewis Moten‘s DataFile.asp file to work but there is no need to modify it. The modifications have been made in my code to make it work with that file as is.
<% Dim oConn Dim oRS Dim sSQL Dim lngFileID Set oConn = Server.CreateObject("ADODB.Connection") oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" _ & Server.MapPath("db\Files.mdb")) sSQL = "SELECT FileID, FileName, Date FROM Files " _ & "ORDER BY Date desc, FileName" Set oRS = oConn.Execute(sSQL) If oRS.EOF Then Response.Write("There are " _ & "currently no files in the database.
") Else Do While NOT oRS.EOF lngFileID = oRS("FileID").Value Response.Write("" _ & oRS("FileName").Value & "
") oRS.MoveNext Loop End If oConn.Close Set oRS = Nothing Set oConn = Nothing %>
The only thing that might throw people is that I‘ve changed the path to the database since mine is stored in the "db" directory on my server therefore the path is db/Files.mdb. I‘ve also included an ORDER BY clause in my sql statement to display the files from newest to oldest and then in alpha order by filename. This code can be placed anywhere on your page that you want the files displayed. You can also set it up to display the files in multiple columns across the page in a instead of one long column down the page but you need to add additional "If NOT oRS.EOF Then" code before each new table cell. I‘ve used this code on a page with other asp code so if you use it by itself you‘d need to add the Option Explicit statement at the top.
Thanks for the great asp resource. Many of my peers have recommended your site to me when I‘ve had questions about developing asp solutions for various things on my websites. Have a great day.
Regards,
Bryan Hovey
Thanks Bryan... I‘m sure you‘re not the only one who needs this functionality. A number of our users are sure to find this helpful.
Related Links
"Pure ASP File Upload" on the Visitor Contributions page