ASP Upload Script – Receiving Form Text Fields with File Uploads

The Chilkat Upload ActiveX component (which is entirely freeware) provides the capability to receive both form text items as well as file uploads in a single HTTP POST. This post provides an example. First we show the HTML FORM with both text and file input fields. Then we show the ASP script to receive the POST.

More information about the Chilkat ASP Upload component.

The HTML form to send the upload w/ additional form parameters:

<html>
<head>
<title>ASP Upload with Additional Form Parameters</title>
<body>
<h1>ASP Upload with Additional Form Parameters</h1>

<p>Demonstrates how to pass form parameters in addition to uploading a file.  The ASP code that receives this POST will use the form parameters for the Subject, From, To, and Body of an email.  The uploaded file will be attached to the email.</p>

<form method="POST" enctype="multipart/form-data" action = "http://localhost/aspEmailUpload.asp" >

Subject: <input name="subject" type="text" size=40><br><br>
From Email: <input name="fromAddr" type="text" size=40><br><br>
To Email: <input name="toAddr" type="text" size=40><br><br>

<textarea name="emailBody" rows="6" cols="40">
Type the plain-text body of your email here...
</textarea><br><br>

Add a file attachment to the email:<br>
<input name=attach1 type=file size=20><br><br>

Attachment Filename in Email: <input name="attachFname" type="text" size=40><br><br>

<input type=submit value="Upload and Email">
</form> 

</body>
</html>

ASP Script to Receive the Upload:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%

'  The Chilkat ASP Upload component is freeware.
'  The UploadRcv object receives uploads in ASP.
'  It can be used to save uploaded files to the web server,
'  or to save directly to memory for immediate access.
'  This example streams an uploaded file into memory.
set receiver = Server.CreateObject("Chilkat.UploadRcv")

'  Set a timeout just in case something hangs
'  This is a 20-second timeout:
receiver.IdleTimeoutMs = 20000

'  Consume the upload.  Files are streamed to the UploadDir
success = receiver.Consume()

If (success = 1) And (receiver.NumFilesReceived > 0) Then

    ' The Chilkat Upload component is freeware.  
    ' For demonstration purposes, this example receives the upload 
    ' form params and file and uses the data to send an email.
    ' The Chilkat Email component is not freeware.
    
	'  The mailman object is used for sending and receiving email.
	set mailman = Server.CreateObject("Chilkat.MailMan2")

	'  Any string argument automatically begins the 30-day trial.
	success = mailman.UnlockComponent("30-day trial")
	If (success = 1) Then

		'  Set the SMTP server.
		mailman.SmtpHost = "smtp.chilkatsoft.com"

		'  Set the SMTP login/password (if required)
		mailman.SmtpUsername = "myLogin"
		mailman.SmtpPassword = "myPassword"

		'  Create a new email object
		set email = Server.CreateObject("Chilkat.Email2")

		' We don't use Request.Form.  Instead use receiver.GetParam
		' because the receiver object received the HTTP request.
		email.Subject = receiver.GetParam("subject")
		
		Response.Write "Subject: " & email.Subject & "<p>"
		
		email.Body = receiver.GetParam("emailBody")
		email.From = receiver.GetParam("fromAddr")
		email.AddMultipleTo receiver.GetParam("toAddr")
		
		' Add the uploaded file as an attachment:
		email.AddDataAttachment receiver.GetFileData(0), receiver.GetParam("attachFname")

		'  Call SendEmail to connect to the SMTP server and send.
		'  The connection (i.e. session) to the SMTP server remains
		'  open so that subsequent SendEmail calls may use the
		'  same connection.
		success = mailman.SendEmail(email)
		If (success = 1) Then
			'  Some SMTP servers do not actually send the email until
			'  the connection is closed.  In these cases, it is necessary to
			'  call CloseSmtpConnection for the mail to be  sent.
			'  Most SMTP servers send the email immediately, and it is
			'  not required to close the connection.  We'll close it here
			'  for the example:
			success = mailman.CloseSmtpConnection()
			If (success <> 1) Then
			    Response.Write "Connection to SMTP server not closed cleanly." & "<br>"
			End If

			Response.Write "Mail Sent!" & "<br>"
		else
		    Response.Write mailman.LastErrorText & "<br>"
		End If
	
	else
	    Response.Write "Component unlock failed"
	end if
	
else 
	Response.Write "Failed to receive upload."
end if



%>
</body>
</html>