Digital Colony!

Using ASPHttp To Scrape Data in Classic ASP

The article was written in 2001.

I recently discovered a cool ASP component from Server Objects called AspHTTP. This component allows an ASP page the ability to GET documents via the HTTP protocol. It also can POST data to a remote web page. Why would you need this for an ASP page? The ability to parse data off a web page and place it in your own format is one need. The AspHttp component lets you to pull the remote web page into your code as a string. From there you can use extensive string parsing to extract the data. Copyright Issues

Copyrights are outside the scope of this article. Just be aware that snagging someone else's data may make their legal departments unhappy. If you choose to use someone else's data, getting their permission may be a wise decision. Use this tool for good, not for evil.

Sample Code

<%
Set HttpObj = Server.CreateObject("AspHTTP.Conn") 
HTTPObj.Url = ' enter some URL here 
' fetch the HTML page into the strResult variable 
strResult = HTTPObj.GetURL 
' check for component error 
If Len(HTTPObj.Error) Then 
   Response.Write "ERROR: " & HTTPObj.Error 
Else ' did we retrieve a document? 
  intLength = Len(strResult) 
  If intLength > 0 Then 
    ' proceed with scrape 
  End If 
End If 
%>

Last Words

Data scraping can be an inexact science. If the web site changes their markup, you can find yourself recoding your scrape. Look for an API first before proceeding with a data scrape.

Labels: , ,

 

File Uploading with ASP Components

This article was written in 2001.

Every now and then there comes a time when a web application requires more from a user. It's not enough that the application receives data in the form of user text boxes and drop-down selections. Sometimes, we need the user to upload a file onto the server. But can we trust the user to setup an FTP program, connect to our server, and place the right file into the right folder without touching anything else? Of course not. What we need to a chimp-simple way to allow users to upload a specific type of file into a specific folder.

Requirements

For this scenario I have 3 requirements:

1 - Restrict the user to uploading to a specified directory on my server.

2 - Restrict file size to prevent huge files from filling up my disk space.

3 - Restrict file types. In this example I only will want image files. Any other file type must be rejected.

Using an ASP Component to Handle File Uploads

There are many file upload components that one can purchase or, if you are so inclined, you can roll your own. The 2 most popular ASP file upload components are Software Artisans FileUp and Persists AspUpload. Both handle uploads slightly different. Let's code each of these components to handle the above requirements.

The FORM

The FORM, where the client uploads a file, is identical.
<form name="form" action="upload.asp" enctype="MULTIPART/FORM-DATA" method="POST">
 <input type="file" name="file1"> <input type="submit" value="Upload Files"> 
</form>

Shared Code

Before you pick which component you will be using, here is some common code that both can use. isFileSizeOK handles the file size restrictions. isValidFile is where you'll define what file extensions the client can upload.

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 will 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

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 & " (" & up.TotalBytes & " bytes)"          
       End If       
   Else          
      strUploadStatus1 = "ERROR: This File Type is restricted from uploading: " & filename       
   End If    
 End If    
 Set up = Nothing 
End Sub

Persists AspUpload

With AspUpload, you save the file first, then perform checks. If the file fails the checks, then the code deletes it from the server. This component also has a "SaveToMemory" option, which bypasses the write to the disk until instructed. ASPUpload has built-in image size handling and can detect if a file is an image with the .ImageType property, but for this example we'll use the isValidFile function.
Sub uploadPersists    
  Set up = Server.CreateObject("Persits.Upload.1")    
  up.OverwriteFiles = TRUE    
  up.SetMaxSize intMaxFileSize    
  up.Save uploadFolder    
  For Each File in up.Files       
     fileName = File.ExtractFileName       
     If isValidFile(fileName) Then          
        If isFileSizeOK(File.OriginalSize) Then             
           strUploadStatus2 = "File [" & filename & "] Uploaded Successfully! "          
        Else             
           strUploadStatus2 = "ERROR: File Too Large: " & fileName & " (" & File.OriginalSize & " bytes)"             
           File.Delete          
        End If       
     Else          
        File.Delete          
        strUploadStatus2 = "ERROR: This File Type is restricted from uploading: " & fileName       
     End If    
  Next 
End Sub

Last Words

Both components can do a lot more than what I've demonstrated above. For a more complete list of features, check out the online manuals for Persists AspUpload and Software Artisans FileUp.

This tutorial was expanded for DevGuru.com as A Simple ASP File Upload Application

Labels: , , ,

 

COM Informant for Classic ASP

I currently do business with 4 different web hosts. Each of them has a different subset of 3rd-party ASP components installed on their server. Sometimes they are open with which components are installed, sometimes they aren't. Whenever I wanted to test to see if a particular COM object was available, I'd write a quick script. The script would try to create the object via the Server.CreateObject method and then I'd go to the page to see if it returned an error code. No error code meant it was installed and I could start coding my application around that knowledge.

Tedious and Repetitive

After about the 10th script I wrote, it hit me that there probably is a better way to do this. What was needed was a script that tested the most common ASP components and allowed the user to quickly add new ones to the list. Having a bunch of test scripts lying on the file server wasn't optimal. And the last thing you would want is to hard-code all the test cases inside your ASP code. My solution was to have a single page handle the creation, modification, and display of component test cases. The data source would be a single XML file.

Sample comlist.xml file

<?xml version="1.0"?> 
<comlist> 
<com company="Microsoft" name="CDONTS" id="CDONTS.NewMail"/> 
<com company="Persit Software Inc." name="ASPUpload" id="Persits.Upload"/> 
</comlist>

Developers Tool

COM Informant is handy tool to have if your development team creates custom components and then deploys them across multiple servers. What better way to test if a component is installed than viewing a single web page. The top portion of the tool allows the user to add any component name to test list.

screen shot

The Download

COM Informant For Classic ASP consists of sniff.asp, comlist.xml, com.css, and blue.gif. This tool will be ready to run after download. You need to be running IIS in order to download and run COM Informant. Also, if you want to add or delete components from the list, then the comlist.xml file must reside in a directory that has read/write permission. You can modify the location of that file within the first 3 lines of code. The code performs some XML file manipulation, which is outside the scope of this article.

This article was first written in 2001

Labels: , ,

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.