Link to home
Start Free TrialLog in
Avatar of Bob Stone
Bob StoneFlag for United States of America

asked on

Upload an image on form and email it

I downloaded Pure ASP file upload from www.motobit.com and used a script from pantyboy's Q at https://www.experts-exchange.com/questions/22482550/Upload-an-image-on-a-form-and-email-it.html but when I try it I get a blank page.

Am I missing something?

Script
~~~~~~~~~~~~~~~~~
<%
' Sample file Form-Email.asp
' Let's you send one an email with one or more attachments.
' Original code from http://www.motobit.com/help/scptutl/upload.asp
' Alteration of code to use CDOSYS instead of CDONTS by fuzzboxer at Experts Exchange
' See https://www.experts-exchange.com/questions/22482550/Upload-an-image-on-a-form-and-email-it.html

'Global declarations.
'Get temporary folder
Dim ResultHTML

'Create upload form
'Using Huge-ASP file upload
'Dim Form: Set Form = Server.CreateObject("ScriptUtils.ASPForm")
'Using Pure-ASP file upload
Dim Form: Set Form = New ASPForm %><!--#INCLUDE FILE="_upload.asp"--><%

Server.ScriptTimeout = 1000
'Do not upload data greater than 10MB.
Form.SizeLimit = &HA00000

'Progress bar window will receive the same ID.
Form.UploadID = Request.QueryString("UploadID")'{/b}

Const fsCompletted  = 0

If Form.State = fsCompletted Then 'Completted
  ResultHTML = ProcessForm
ElseIf Form.State > 10 then
  Const fsSizeLimit = &HD
  Select case Form.State
            case fsSizeLimit: ResultHTML = "<br><Font Color=red>Source form size (" & Form.TotalBytes & "B) exceeds form limit (" & Form.SizeLimit & "B)</Font><br>"
            case else ResultHTML = "<br><Font Color=red>Some form error.</Font><br>"
  end Select
End If

if request.QueryString("Action") = "Cancel" then
      ResultHTML = "<br><b>Upload was cancelled</b>"
end if

Function TempFolder()
      Dim FS
  Set FS = CreateObject("Scripting.FileSystemObject")
  'Get temporary folder
  TempFolder = FS.GetSpecialFolder(2) & "\emailtemp"
End Function

Sub DeleteFile(FileName)
      Dim FS
  Set FS = CreateObject("Scripting.FileSystemObject")
      FS.DeleteFile FileName
End Sub

Function ProcessForm
  Dim eName, eAddress, ePostcode, ePhone, eMail, eStory

  'get source form fields - From, To, Subject, Message etc.
  eName = Form("Name")
  eAddress = Form("Address")
  ePostcode = Form("PostCode")
  ePhone = Form("PhoneNumber")
  eMail = Form("EmailAddress")
  eStory = Form("Story")
 
  Dim HTML
  HTML = "<br><span style='color:red'>Server-side ASP script accepted source form with fields and files and email object was created. "
  HTML = HTML & "<br>From: <b>" & eName & "</b>"
  HTML = HTML & "<br>Address: <b>" & eAddress & "</b>"
  HTML = HTML & "<br>Postcode: <b>" & ePostcode & "</b>"
  HTML = HTML & "<br>Phone: <b>" & ePhone & "</b>"
  HTML = HTML & "<br>Email: <b>" & eMail & "</b>"
  HTML = HTML & "<br>Story: <b>" & eStory & "</b>"

  Dim objNewMail, File, FileName

  'Create a new email message
 Set objNewMail = CreateObject("CDO.Message")
  objNewMail.To = "name@domain.com" ' Change this to your recipient
  objNewMail.From = "name@domain.com" ' Change this to your poster
  objNewMail.Subject = "From the Website" ' Change this to your subject line
  'Save source files to temporary folder
  'Add these files to the new e-mail
      HTML = HTML & "<br>Attachments:"
  For Each File In Form.Files.Items

    'If source file is specified.
    If Len(File.FileName) > 0 Then

              HTML = HTML & "<br>&nbsp;" & File.Name & ": <b>" & File.FileName & ", " & File.Length \ 1024 & "kB</b>"
      FileName = TempFolder & "\" & File.FileName
   
      File.SaveAs FileName
     
      objNewMail.AddAttachment FileName
    End If
  Next
 
  'Send the new email
  objNewMail.HtmlBody = HTML
  objNewMail.Send
 
  Response.Redirect("ThankYou.asp") ' Change this to your page name
 
  Set objNewMail=nothing
 
  'delete temporary files
  For Each File In Form.Files.Items
    If Len(File.FileName) > 0 Then
      FileName = TempFolder & "\" & File.FileName
      on error resume next
      DeleteFile FileName
    End If
  Next
  HTML = HTML & "</Font><br>"
  ProcessForm = HTML
End Function

'{b}get an unique upload ID for this upload script and progress bar.
Dim UploadID, PostURL
UploadID = Form.NewUploadID

'Send this ID as a UploadID QueryString parameter to this script.
PostURL = Request.ServerVariables("SCRIPT_NAME") & "?UploadID=" & UploadID'{/b}
%>
Avatar of dodge20
dodge20

Do you have write permissions set on your emailtemp folder?
Also double check the ACTION attribute within the <FORM> tag to make sure the page posts back to itself, or wherever your above code is located.
Hi S

Did you remember to put the place to files '_upload.asp' and 'progress.asp' into your directory? Did you include the scripts at the end of the form?

I've checked back to our posts and I noticed that I didn't post the form I'm using, so here it is. I may give you a clue.

<form method="post" ENCTYPE="multipart/form-data" OnSubmit="return ProgressBar();" Action="<%=PostURL%>">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
      <tr>
      <td width="100" align="left" nowrap="nowrap">Full Name:</td>
      <td align="left" width="100%"><input name="Name" type="text" class="inputBox" value="<%=Form("Name")%>" /></td>
      </tr>
      <tr>
      <td align="left" valign="top">Full Address:</td>
      <td align="left"><textarea name="Address" rows="5" class="inputBox" value="<%=Form("Address")%>"></textarea></td>
      </tr>
      <tr>
      <td align="left">Postcode:</td>
      <td align="left"><input name="PostCode" type="text" class="inputBox" value="<%=Form("PostCode")%>" /></td>
      </tr>
      <tr>
      <td align="left">Phone Number:</td>
      <td align="left"><input name="PhoneNumber" type="text" class="inputBox" value="<%=Form("PhoneNumber")%>" /></td>
      </tr>
      <tr>
      <td align="left">Email Address:</td>
      <td align="left"><input name="EmailAddress" type="text" class="inputBox" value="<%=Form("EmailAddress")%>" /></td>
      </tr>
      <tr>
      <td colspan="2">Your Story:</td>
      </tr>
      <tr>
      <td colspan="2"><textarea name="Story" rows="10" class="inputBox" value="<%=Form("Story")%>"></textarea></td>
      </tr>
      <tr>
      <td colspan="2">Add a photo to illustrate your story:
     <!--Form size limit is <%=Form.SizeLimit%>B (<%=Form.SizeLimit \ 1024 %>kB).<br>-->
    <DIV ID=files>
      <input type="file" name="Photo" class="inputBox">
    </DIV>
      </td></tr>
      <tr>
      <td colspan="2">
            <table width="100%">
                  <tr>
                  <td width="50%" align="right" style="padding-right:10px"><input type="submit" name="Submit" value="Submit" /></td>
                  <td width="50%" align="left" style="padding-left:10px"><input type="reset" name="Reset" value="Reset" /></td>
                  </tr>
            </table>
      </td>
      </tr>
</table>
</form>

<SCRIPT>
//Script to add an attachment file field
var nfiles = 1;
function Expand(){
  nfiles++
  var adh = '<BR> Attachment '+nfiles+' : <input type="file" name="File '+nfiles+'">';
  files.insertAdjacentHTML('BeforeEnd',adh);
  return false;
};

//Open a window with progress bar indicator.
function ProgressBar(){
  var ProgressURL
  ProgressURL = 'progress.asp?UploadID=<%=UploadID%>'

  var v = window.open(ProgressURL,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=350,height=200')
 
  return true;
}
</SCRIPT>


Sorry S

Just realised that my first line is nonsense.

It should have said, Did you remember to place the files '_upload.asp' and 'progress.asp' into your directory?

Martin
Avatar of Bob Stone

ASKER

I must be dense, I can't get this to work.

http://kansasstamps.org/state/mailer/ shows up weird.
Post the whole page here.

I want to make sure there's nothing missing because all I get when I view the source is the form.

I can also compare it side-by-side with my own page. Maybe that will show something.

Martin
Nevermind, it was a D'OH type mistake. I got a few permissions probs to work out tho.
The ASP code is not going to render viewable in the source code, Pantyboy.
ASKER CERTIFIED SOLUTION
Avatar of Martin Cotterill
Martin Cotterill
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial