ASP instance: 6 ASP programs commonly used in Dynamic Web pages

Source: Internet
Author: User
Tags character set file size html page iis odbc odbc connection ole zip
Program | news | Web page

1. The following code shows how to obtain x,y coordinates from a picture in the client browser on the server side, noting that the type of the input control is an image type.

<form>
<input name= "ImageMap" type= "Image" src= "Http://www.webjx.com/htmldata/2007-06-14/ImageMap.jpg" click Anywhere ">
</form>
<%imagemap.x = <%=request ("imagemap.x")
IMAGEMAP.Y = <%=request ("imagemap.y")%>

2. Using the ADODB.stream object, download the various files on the server in IE browser.

The user is prompted to download the file directly instead of the browser. Note that after copying the following code into an ASP file, do not add some non-ASP code to the page: code such as HTML and JavaScript client.

<%
'--------------------------------------------
Response.Buffer = True
Dim strFilePath, Strfilesize, strFileName
Const adTypeBinary = 1
strFilePath = "File path"
Strfilesize = ... File size, optional
strFileName = "FileName"
Response.Clear
' 8*******************************************8
' Need to install MDAC 2.6 or MDAC2.7 on your server
' 8*******************************************8
Set objstream = Server.CreateObject ("ADODB. Stream ")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
Strfiletype = LCase (Right (strFileName, 4)) ' File name extension
' Judge content-types by file name extension
Select Case Strfiletype
Case ". asf"
ContentType = "VIDEO/X-MS-ASF"
Case ". avi"
ContentType = "Video/avi"
Case ". Doc"
ContentType = "Application/msword"
Case ". zip"
ContentType = "Application/zip"
Case ". xls"
ContentType = "Application/vnd.ms-excel"
Case ". gif"
ContentType = "Image/gif"
Case ". jpg", "JPEG"
ContentType = "Image/jpeg"
Case ". wav"
ContentType = "Audio/wav"
Case ". mp3"
ContentType = "Audio/mpeg3"
Case ". mpg", "MPEG"
ContentType = "Video/mpeg"
Case ". rtf"
ContentType = "Application/rtf"
Case ". htm", "html"
ContentType = "Text/html"
Case ". asp"
ContentType = "Text/asp"
Case Else
' Handle all other Files
ContentType = "Application/octet-stream"
End Select
Response.AddHeader "Content-disposition", "attachment"; Filename= strFileName
Response.AddHeader "Content-length", strfilesize
Response.Charset = "UTF-8" client browser's character set UTF-8
Response.ContentType = ContentType
Response.BinaryWrite Objstream.read
Response.Flush
objStream.Close
Set objstream = Nothing
%>

3. Improve the response rate of ASP pages

On the first line of your ASP page, add:

<% EnableSessionState = False%>

This closes the session object and increases your server response rate, and the more common problem is that an HTML page contains two frames pages (at least one is an ASP page and uses a session), which makes it necessary to wait on a frames page (of course this box
When the session is used in the rack page, the other frames page will not be displayed until the load is finished.

If you use proxy access, by default, many proxy servers do not dynamically cache ASP page content, adding the following code:

<%
Response.CacheControl = "Public"
%>

This line of code caches ASP pages on the proxy server, speeding up the response rate of the client requesting dynamic pages, and some infrequently changing ASP pages are made directly from the proxy server.

4. To know that the browser (ie, for example) will not parse return and newline characters, if you use the Response.Write method to write a line containing a carriage return and newline characters of the string to the dynamic page, the results can be imagined, you need to do is:

<%
Response.Write (Replace (Body, vbCrLf, "<br>"))
%>

Use <br> to replace carriage return and line wrapping. Note: If the carriage return and newline characters appear in the Input/textarea and other controls in the form, you do not have to do so.

5. Write IIS log with ASP code

<%
Response.appendtolog "database is being accessed"
%>

After executing this code, the following string may appear in your IIS log:

127.0.0.1,--01/01/00, 12:00:34, W3svc1,webserver,
127.0.0.1, 161342, 485, 228, 0, GET,/somefile.asp, database is being accessed

Note: Because the contents of the log file are separated by commas, the log content written should avoid using commas.

6. How to access the MDB database file on a remote computer

If you use an ODBC connection (DSN method or otherwise) to an MDB file on a remote computer, this will produce an error:
Microsoft OLE DB Provider for ODBC Drivers error ' 80004005 ' roughly means that the file may be accessed by another user or without sufficient permissions.

Here are two ways to avoid this error:

Way A. Using the DAO engine to access

Dim File, Conn, RS
Const ReadOnly = False
File = "\\server\share\file.mdb"
Set Conn = CreateObject ("DAO. dbengine.35 "). Workspaces (0). OpenDatabase (file,,readonly)
Set RS = Conn.openrecordset (SQL)

Mode B. ADO + Jet OLE DB provider method

Dim Conn, RS
Set Conn = CreateObject ("ADODB. Connection ")
Conn.provider = "Microsoft.Jet.OLEDB.4.0"
Conn.Open "\\server\share\file.mdb"
Set RS = Conn.execute (SQL)

Make sure that you have sufficient access rights to access the MDB file on the remote computer when you run the ASP page, and you need to log on to the remote computer before you can access the MDB file and add the following code

Set UM = CreateObject ("Usermanager.server")
Rmi LogonUser "Account", "Password", "Domain"
...
Open Database
...
Rmi RevertToSelf



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.