Solved

attachments

Posted on 2002-05-14
27
557 Views
Last Modified: 2012-06-27
I am developing an intranet site.  Is there a way to allow users to attach electronic files to a form and how is this done?  Can it be done without the users having access to the web server or do they have to have access to a particular directory?  Thanks.

(points will be increased based on the replies)
0
Comment
Question by:goon55
  • 8
  • 7
  • 2
  • +6
27 Comments
 
LVL 12

Expert Comment

by:lexxwern
ID: 7008442
well, the file can be attached i think only if the form method is POST.

this will be your html...
<input type="file" name="file"/>


now this should be handled by your server side program. please tell which language do you use.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7008499
What you need is a server side control that retrieves the data from the form and knows how to attach the file. Using standard HTML you will not be able to do this.

Using for example CDONTS, you will be able to email the form and all it's contents and it should also be able to send the attachment along.

Good luck,
Max.
0
 

Author Comment

by:goon55
ID: 7008677
lexxwern:  not sure what you mean by language I use.  I use FP to publish, and asp and javascript to code.  

Max:  Are you saying the only way I can do this is to have a secondary program that runs on the web server?  The problem it, I do not have access to the web server itself.  I do have full control over my web though.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7008689
What exactly do you want to do? Send an email with attachment? Or upload something to the server?

If you want to send an email with attachment..all you need to do, is figure out which email component is used by your hosting provider. If it is CDONTS, I can help you write the code you need to send with attachment. Otherwise I could possibly also help, but with CDONTS I know.

If you want to upload to the server, you will at least need to know the path so you can upload there. I also have sample code for that.

Max.
0
 
LVL 18

Expert Comment

by:bruno
ID: 7008748
clarify your question please...
0
 

Author Comment

by:goon55
ID: 7009254
I want the user to be able to complete a form, attach an electronic file to it, then when they hit submit, have the request and the electronic file be emailed to me.  I will find out which email component we use.  Is there a way I can tell?
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7009457
Usually your provider somewhere has a list of components that you can use...if any...

Max.
0
 

Author Comment

by:goon55
ID: 7009465
We do use CDONTS
0
 
LVL 30

Expert Comment

by:third
ID: 7010073
a sample using CDONTS,

<%
  Option Explicit

  Dim objMail
  Set objMail = Server.CreateObject("CDONTS.NewMail")

  objMail.From = Request("fromfield")
  objMail.Subject = Request("subjectfield")
  objMail.AttachFile(Request("attachfield") )
  objMail.To = Request("tofield")
  objMail.Body = Request("body")
  objMail.Send

  Response.write("Mail was Sent")

  'You should always do this with CDONTS.
  set objMail = nothing
%>

http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=13

the above should be contained in the page to where the form is submitting. you can retrieve the form fields through using Request("formfieldname") if you used POST or Request.Querystring("formfieldname") if you used GET method (but it is usually done in POST).
0
 
LVL 10

Expert Comment

by:dij8
ID: 7010606
You will also need to add to your form tag enctype="multipart/form-data".  And collecting a file can ONLY be done using the POST method.
0
 

Author Comment

by:goon55
ID: 7010935
I actually saw that tutorial out there and I have been trying to get it to work, but I am getting the following error:


HTTP Error 405
405 Method Not Allowed

The method specified in the Request Line is not allowed for the resource identified by the request. Please ensure that you have the proper MIME type set up for the resource you are requesting.

Please contact the server's administrator if this problem persists.

Here is my code:

<%


Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
%>

<%
objCDO.To = "abc@abc.com"
objCDO.Subject = "Subject"
objCDO.Send 'Send off the email!

'Cleanup
Set objCDO = Nothing

%>
<html>

<head>
<title>New Page 1</title>
</head>

<body>
<form method="post">
    <b>Enter your email address:</b><br>
    <input type="text" name="txtEmail" value="">
    <p>
    <input type="submit" value="Send me an Email with an Attachment!">
  </form>
</form>
</body>

</html>

I think I am missing something in the syntax.  I guess I am not sure how to implement the code either.  The error is a server error right?  Could it be an access issue?

Thanks

0
 

Author Comment

by:goon55
ID: 7010960
I actually saw that tutorial out there and I have been trying to get it to work, but I am getting the following error:


HTTP Error 405
405 Method Not Allowed

The method specified in the Request Line is not allowed for the resource identified by the request. Please ensure that you have the proper MIME type set up for the resource you are requesting.

Please contact the server's administrator if this problem persists.

Here is my code:

<%


Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
%>

<%
objCDO.To = "abc@abc.com"
objCDO.Subject = "Subject"
objCDO.Send 'Send off the email!

'Cleanup
Set objCDO = Nothing

%>
<html>

<head>
<title>New Page 1</title>
</head>

<body>
<form method="post">
    <b>Enter your email address:</b><br>
    <input type="text" name="txtEmail" value="">
    <p>
    <input type="submit" value="Send me an Email with an Attachment!">
  </form>
</form>
</body>

</html>

I think I am missing something in the syntax.  I guess I am not sure how to implement the code either.  The error is a server error right?  Could it be an access issue?

Thanks

0
 
LVL 19

Expert Comment

by:webwoman
ID: 7010979
The form field you need is called FILE, and it generates a browse button (maybe a text field, not sure since I rarely use it).

You can't attach from the CLIENT with ASP, only from the SERVER. You'll need a component on the server if you want to allow the client to attach a file to their form.

There are some free ones, I think ASPUpload is one that's mentioned a lot. The server needs to be set up to use it, and there needs to be a folder to accept the uploads.

What you have will just send nothing -- you haven't put the form contents in the e-mail, there's no BODY. Nor have you put a FROM address, and CDONTS needs that.

Check www.asp101.com they have a lot of good sample scripts, tutorials, and components there.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:goon55
ID: 7011111
OK, sorry, that was old code.  Here is the real code I have, but I am still getting the same error.

Set objCDO = Server.CreateObject("CDONTS.NewMail")
%>

<%
objCDO.To = "abc@abc.com"
objCDO.From = "xyz@xyz.com"
objCDO.Subject = "Subject"

Dim fileAttach
fileAttach=Request("attachment")

objCDO.Body = "fileAttach"

objCDO.Send

%>
<html>

<head>
<title>New Page 1</title>
</head>

<body>
<form method="post">
    <b>Attach your file:</b><br>
    <input type="file" name="attachment">
    <p>
    <input type="submit" value="Submit">
  </form>
</form>
</body>

</html>


I do not have access to install anything on the web server.  How would I go about getting the component installed?
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7011219
just change

objCDO.Body = "fileAttach"

to

objCDO.AttachFile(fileAttach)

That should work for you...

Max.
0
 

Author Comment

by:goon55
ID: 7011685
I am still getting the same error with this code:

<%
Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
%>

<%
objCDO.To = "abc@abc.com"
objCDO.From = "xyz@xyz.com"
objCDO.Subject = "Subject"

Dim fileAttach
fileAttach=Request("attachment")

objCDO.AttachFile(fileAttach)

objCDO.Send

%>

Is this the way said to change it?
0
 
LVL 19

Expert Comment

by:webwoman
ID: 7012537
No. You can't attach anything that doesn't exist. That attachment doesn't exist. And who is attaching things? That's the SERVER attaching something that's being send to abc@abc.com. That has NOTHING to do with the client uploading a file. (And you still don't have a BODY to this mail)

YOu can't do this all in one. You need a form that is sent to the server, and the client attachs the file to be sent. You will need a component, it's not good to try to do it in just straight asp.

The SERVER generates the e-mail, with the info passed from the form. Unless you want the server to send the file right back to the client (which isn't what I think you want), you're doing this sideways.

What you want to do has NOTHING to do with CDONTS. You're not GENERATING the e-mail, you're receiving the file and the other info from the form. Totally different thing. ;-)

Now, if you want to generate a confirmation e-mail back to the client, that's where you'd use CDONTS. You'd also give the mail some content...it needs a BODY. Right now you've got a head and arms, but no body. ;-)

The server administrator would need to install any components on the web server. If you don't have total control over it (you're hosting this somewhere, not under your control at all) you may have to see what they will allow. If this is an intranet, the server administrator will be more than happy to install any reliable upload component, especially one that's free (they exist), because it will considerably cut down on the strain on the server.
0
 
LVL 19

Accepted Solution

by:
DreamMaster earned 50 total points
ID: 7013132
Yes, webwoman is right, you need more steps, I completely forgot that in the process...

What you need to understand is that when you try to attach the file to the mail using CDONTS, and the location is for example "c:\my documents\somefile.doc" (just to name an example) the CDONTS component looks for the file on the local C drive of the server, not the client. So what you'd need to do first, is upload the file to the server, and remember that location for use with CDONTS.

Something like this:

Let's say your form file looks like this:

[form.html]
<HTML>
<BODY>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="uploadscript1.asp">
Naam: <INPUT TYPE="Text" NAME="Naam"><BR>
Bestand: <INPUT TYPE="file" NAME="file1"><BR>
<INPUT TYPE="submit" Value="Submit">
</FORM>
</BODY>
</HTML>

Pretty simple right?

Now to get the file onto the server:

[uploadscript1.asp]

<%
Option Explicit

Dim oUpload
Dim oFSO, oFile
Dim lngPathEnd
Dim strPath, strFile
Dim i

'Create the object, mind the New method instead of
'Server.CreateObject
Set oUpload = New FileUpload

'Get Path and File name
lngPathEnd = Len(Server.MapPath(Request.ServerVariables("PATH_INFO"))) - 14
strPath = Left(Server.Mappath(Request.ServerVariables("PATH_INFO")), lngPathEnd)
strFile = "uploaded" & oUpload.FileName("bestand")

'Make file and write the data to it.
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strPath & strFile)
For i = 1 To LenB(oUpload.Value("file1"))
   oFile.Write Chr(AscB(MidB(oUpload.Value("file1"), i, 1)))
Next

'Close the file and free the objects
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing

Response.Write "Thank you for uploading " & strPath & strFile

Set oUpload = Nothing
%>
<!--#include file="clsFileUpload.asp"-->

This can upload the file without the use of a component.

Now you'll need the file clsFileUpload.asp

[clsFileUpload.asp]
<%
Class FileUpload

   Private pvObjUploadRequest
   
   Private Sub Class_Initialize
      Dim RequestBin, Boundary, Value
      Dim lngPosBegin, lngPosEnd, lngBoundaryPos
      Dim lngPos, lngPosFile, lngPosBound
      Dim strName, strFileName, strContentType
      Dim objUploadControl

      'Initialize common directory
      Set pvObjUploadRequest = Server.CreateObject("Scripting.Dictionary")
     
      'Request Binary Data
      RequestBin = Request.BinaryRead(Request.TotalBytes)
     
      'Distract Binary Data
      lngPosBegin = 1
      lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))
      Boundary = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
      lngBoundaryPos = InStrB(1, RequestBin, Boundary)

      'Request all data
      Do Until (lngBoundaryPos = InStrB(RequestBin, Boundary & getByteString("--")))
         'Variables of objects get saved in dictionary
         Set objUploadControl = Server.CreateObject("Scripting.Dictionary")

         'Request object name
         lngPos = InStrB(lngBoundaryPos, RequestBin, GetByteString("Content-Disposition"))
         lngPos = InStrB(lngPos, RequestBin, GetByteString("name="))
         lngPosBegin = lngPos + 6
         lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
         strName = LCase(GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)))
         lngPosFile = InStrB(lngBoundaryPos, RequestBin, GetByteString("filename="))
         lngPosBound = InStrB(lngPosEnd, RequestBin, Boundary)

         'See if the object is of type file
         If lngPosFile <> 0 And lngPosFile < lngPosBound Then
            'Get filename, content-type and contents
            lngPosBegin = lngPosFile + 10
            lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(34)))
            strFileName = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))

            'Add filename to Dictionary
            objUploadControl.Add "FileName", strFileName
            lngPos = InStrB(lngPosEnd, RequestBin, GetByteString("Content-Type:"))
            lngPosBegin = lngPos + 14
            lngPosEnd = InStrB(lngPosBegin, RequestBin, GetByteString(Chr(13)))

            'Add Content-type to Dictionary
            strContentType = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
            objUploadControl.Add "ContentType", strContentType

            'Get contents of file
            lngPosBegin = lngPosEnd + 4
            lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
            Value = MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin)
         Else
            'Get contents of object
            lngPos = InStrB(lngPos, RequestBin, GetByteString(Chr(13)))
            lngPosBegin = lngPos + 4
            lngPosEnd = InStrB(lngPosBegin, RequestBin, Boundary) - 2
            Value = GetString(MidB(RequestBin, lngPosBegin, lngPosEnd - lngPosBegin))
         End If

         'Add contents to Dictionary
         objUploadControl.Add "Value" , Value  

         'Add Dictionary to common Dictionary
         pvObjUploadRequest.Add strName, objUploadControl

         'Loop to next object
         lngBoundaryPos = InStrB(lngBoundaryPos + LenB(Boundary), RequestBin, Boundary)
      Loop
   End Sub


   Private Sub Class_Terminate
      Dim objDictionary

      'Empty all objects and free them
      For Each objDictionary In pvObjUploadRequest.Items
         objDictionary.RemoveAll
         Set objDictionary = Nothing
      Next
      pvObjUploadRequest.RemoveAll
      Set pvObjUploadRequest = Nothing
   End Sub


   Private Function GetByteString(strString)
   'Convert String to Byte String
      Dim Char
      Dim i

      For i = 1 To Len(strString)
          Char = Mid(strString, i , 1)
         GetByteString = GetByteString & ChrB(AscB(Char))
      Next
   End Function


   Private Function GetString(strBin)
   'Convert Byte String to String
      Dim intCount

      GetString = ""

      For intCount = 1 To LenB(strBin)
         GetString = GetString & Chr(AscB(MidB(strBin, intCount, 1)))
      Next
   End Function


   Public Function Value(Name)
   'Get value of form field
      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         Value = pvObjUploadRequest.Item(Name).Item("Value")
      Else
         Value = Empty
      End If
   End Function


   Public Function ContentType(Name)
   'Get ContentType of file form field
      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         If pvObjUploadRequest.Item(Name).Exists("ContentType") Then
            ContentType = pvObjUploadRequest.Item(Name).Item("ContentType")
         Else
            ContentType = Empty
         End If
      Else
         ContentType = Empty
      End If
   End Function


   Public Function FileNamePath(Name)
   'Get Filename+Path opvragen of file form field
      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         If pvObjUploadRequest.Item(Name).Exists("FileName") Then
            FileNamePath = pvObjUploadRequest.Item(Name).Item("FileName")
         Else
            FileNamePath = Empty
         End If
      Else
         FileNamePath = Empty
      End If
   End Function


   Public Function FileName(Name)
   'Get filename of file form field
      Dim strFileName

      Name = LCase(Name)
      If pvObjUploadRequest.Exists(Name) Then
         If pvObjUploadRequest.Item(Name).Exists("FileName") Then
            strFileName = pvObjUploadRequest.Item(Name).Item("FileName")
            FileName = Right(strFileName, Len(strFileName) - InstrRev(strFileName, "\"))
         Else
            FileName = Empty
         End If
      Else
         FileName = Empty
      End If
   End Function
End Class
%>

The other step you'll need is to hold the filename as it will be on the server, path and filename and attach that to the CDONTS object as you did before.

Good luck,
Max.
0
 

Author Comment

by:goon55
ID: 7013569
Wow, holy code!  Thanks!  Ok, I have the three pages set up like above, however, when I attach a file and hit submit, I get the following error:

     Microsoft VBScript runtime error '800a01f4'

     Variable is undefined: 'FileUpload'

     /isgsurveys/uploadscript1.asp, line 12


Also, where would I implement the CDONTS code and to which pages?
0
 
LVL 18

Expert Comment

by:bruno
ID: 7013817
Max might have forgotten to dim that variable, try dimming it on whatever page is giving you that error.


BRUNO
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7013890
No Bruno...the include line is on the wrong line...LOL...including it at the end of the uploadscript1.asp file won't do a lot of good as it needs the variable in that file.

So if you put the line:

<!--#include file="clsFileUpload.asp"-->

On the top of the file uploadscript1.asp, even before any other code is loaded, so:

<!--#include file="clsFileUpload.asp"-->
<%
Option Explicit

Dim oUpload
Dim oFSO, oFile
Dim lngPathEnd
Dim strPath, strFile
Dim i
....
and so on...

It should know the class it is referring to.

Oh and goon55, you could send the mail using CDONTS at the end of that very same file. Because you are dimming the strPath and strFile at the beginning of this ASP file, they will be known all over this file and you can use them also at the end. Allowing you to send the file from the server path instead of client side.

Good luck,
Max.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7117407
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
   In the absence of responses, I will recommend DELETE unless it is clear
   to me that it has value as a PAQ.  Silence = you don't care
</note>

Cd&
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7120503
I think my comments were enough to help goon55 with his problem...

Max.
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 7121175
yes. points to max.
0
 
LVL 10

Expert Comment

by:dij8
ID: 7121285
No arguments here.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7130722
It is time to clean this abandoned question up.  

I am putting it on a clean up list for CS.

<recommendation>
points to DreamMaster

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7179060
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DNS, website, godaddy 6 41
logo disappears on cell phones 5 32
comparing strings that contain line breaks 18 35
Hovering effect 9 29
Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Read about why website design really matters in today's demanding market.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now