473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

File Upload

I came across this article titled Pure ASP File Upload at
http://www.asp101.com/articles/jacob/scriptupload.asp & tried it on my
local IIS5.1. Intranet server & it worked. I saved the necessary data
in a MS-Access database file (& not in the hard disk).

Can someone please tell me how do I mail the uploaded file as an
attachment using CDO.MESSAGE?

I tried getting in touch with Jacob, author of the article at the
above URL, by e-mailing him at his e-mail id (which is given in the
article) but unfortunately, the mail bounced back saying delivery
failed!

Thanks

Jun 11 '07 #1
8 4075
rn**@rediffmail.com wrote:
I came across this article titled Pure ASP File Upload at
http://www.asp101.com/articles/jacob/scriptupload.asp & tried
it on my local IIS5.1. Intranet server & it worked. I saved the
necessary data in a MS-Access database file (& not in the hard
disk).

Can someone please tell me how do I mail the uploaded file as
an attachment using CDO.MESSAGE?
This is fairly simple. You need an ASP script that will stream the file to a
browser, based on some key to the DB record. Then you construct a URL for
use in the AddAttachment method of the CDO.Message object.

IMessage.AddAttachment(URL,[UserName],[Password]) ...

"The URL prefixes supported in the URL parameter are file://,
ftp://, http://, and https://. The default prefix is file://.
This facilitates designation of paths starting with drive
letters and of universal naming convention (UNC) paths."

http://msdn2.microsoft.com/en-us/library/ms526983.aspx
To give you an example, here is a segment of code from a feedback form I
use. In it, I call a stored procedure that returns a list of files to be
attached to the message, then makes successive calls to AddAttachment():

for (CN.File_ListByRequest(GUID,"Feedback","Attachment ",RS); !RS.EOF;
RS.MoveNext())
MSG.AddAttachment([My script URL] + "?RecordID=" +
RS.Fields("RecordID").Value)

Those calls, in turn, trigger this script (excerpted, of course):

var RecordID = Request.QueryString("RecordID").Item,
CN = Server.CreateObject("ADODB.Connection"),
RS = Server.CreateObject("ADODB.Recordset")
CN.Open(*** my connection string ***)
CN.File_Retrieve(RecordID,RS)
if (!RS.EOF) {
Response.AddHeader("Content-Length",RS.Fields("FileSize").Value)
Response.AddHeader("Content-Disposition","inline; filename=" +
RS.Fields("FileName").Value + ";")
Response.ContentType = RS.Fields("ContentType").Value
Response.BinaryWrite(RS.Fields("Data").Value)
}
RS.Close()
CN.Close()

As you may have guessed, these are JScript examples.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Jun 11 '07 #2

"Dave Anderson" <NP**********@spammotel.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
rn**@rediffmail.com wrote:
I came across this article titled Pure ASP File Upload at
http://www.asp101.com/articles/jacob/scriptupload.asp & tried
it on my local IIS5.1. Intranet server & it worked. I saved the
necessary data in a MS-Access database file (& not in the hard
disk).

Can someone please tell me how do I mail the uploaded file as
an attachment using CDO.MESSAGE?

This is fairly simple. You need an ASP script that will stream the file to
a
browser, based on some key to the DB record. Then you construct a URL for
use in the AddAttachment method of the CDO.Message object.

IMessage.AddAttachment(URL,[UserName],[Password]) ...

"The URL prefixes supported in the URL parameter are file://,
ftp://, http://, and https://. The default prefix is file://.
This facilitates designation of paths starting with drive
letters and of universal naming convention (UNC) paths."

http://msdn2.microsoft.com/en-us/library/ms526983.aspx
To give you an example, here is a segment of code from a feedback form I
use. In it, I call a stored procedure that returns a list of files to be
attached to the message, then makes successive calls to AddAttachment():

for (CN.File_ListByRequest(GUID,"Feedback","Attachment ",RS); !RS.EOF;
RS.MoveNext())
MSG.AddAttachment([My script URL] + "?RecordID=" +
RS.Fields("RecordID").Value)

Those calls, in turn, trigger this script (excerpted, of course):

var RecordID = Request.QueryString("RecordID").Item,
CN = Server.CreateObject("ADODB.Connection"),
RS = Server.CreateObject("ADODB.Recordset")
CN.Open(*** my connection string ***)
CN.File_Retrieve(RecordID,RS)
if (!RS.EOF) {
Response.AddHeader("Content-Length",RS.Fields("FileSize").Value)
Response.AddHeader("Content-Disposition","inline; filename=" +
RS.Fields("FileName").Value + ";")
Response.ContentType = RS.Fields("ContentType").Value
Response.BinaryWrite(RS.Fields("Data").Value)
}
RS.Close()
CN.Close()

As you may have guessed, these are JScript examples.
Alternatively, instead of using a second request to the server, create the
attachment 'manually' like this:-

CN2 = Server.CreateObject("ADODB.Connection")
CN2.Open(*** my connection string ***)
RS2 = Server.CreateObject("ADODB.Recordset")

for (CN.File_ListByRequest(GUID,"Feedback","Attachment ",RS); !RS.EOF;
RS.MoveNext())
{
CN2.File_Retrieve(RS.Fields("RecordID").Value, RS2)
if (!RS2.EOF) addAttachment(MSG, RS)
RS2.Close()
}
CN2.Close()

function addAttachment(voMsg, voRS)
{
var oPart = oMsg.Attachments.Add()
oPart.ContentMediaType = voRS.Fields("ContentType").Value
oPart.ContentTransferEncoding = "base64"
oPart.Fields("urn:schemas:mailheader:content-disposition") =
'attachment; filename="' + voRS.Fields("FileName").Value + '"'
oPart.Fields.Update()

var oStream = oPart.GetDecodedContentStream()

oStream.Write(voRS.Fields("Data").Value)
oStream.Flush()
}
Use of an HTTP URL in AddAttachment when running in ASP is undesirable since
CDO uses the WinINET api to perform the fetch. WinInet is not thread safe.
Self referencing URLs can also be a problem if used too often.

BTW, Don't use AddHeader to add a Content-Length header, IIS/ASP will handle
it so at best you're duplicating a header.

Jun 13 '07 #3
Anthony Jones wrote:
Use of an HTTP URL in AddAttachment when running in ASP is
undesirable since CDO uses the WinINET api to perform the
fetch. WinInet is not thread safe. Self referencing URLs
can also be a problem if used too often.
Nice explanation, Anthony. I have since incorporated this technique.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Jun 13 '07 #4
"Anthony Jones" wrote:
>The line in question is the Stream.Write() line.

First experiment would be to split the stream write line:-

Dim fieldValue
fieldValue = voRS.Fields("Data").Value
oStream.Write(fieldValue)
I should have been more clear. I have already isolated it to Stream.Write()
The only time I've had CDO object to this approach is when trying to
attach another eml in which case the content type is message/rfc822,
it refuses to use base64 encoding and requires a text based encoding.
Hence another clutching a straws exercise might be to see if a failing
attachment works if the content type is set to application/octet-stream.
However I honest don't think that is the problem.
Either way I look at it, I have a problem. I can attempt to attach files
with Message.Attachments.Add() and streams, then start over with
Message.AddAttachment() if it fails, but that isn't a very satisfying
compromise.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Jul 30 '07 #5
"Dave Anderson" <NP**********@spammotel.comwrote in message
news:Oa**************@TK2MSFTNGP02.phx.gbl...
"Anthony Jones" wrote:
The line in question is the Stream.Write() line.
First experiment would be to split the stream write line:-

Dim fieldValue
fieldValue = voRS.Fields("Data").Value
oStream.Write(fieldValue)

I should have been more clear. I have already isolated it to
Stream.Write()
>
The only time I've had CDO object to this approach is when trying to
attach another eml in which case the content type is message/rfc822,
it refuses to use base64 encoding and requires a text based encoding.
Hence another clutching a straws exercise might be to see if a failing
attachment works if the content type is set to application/octet-stream.
However I honest don't think that is the problem.

Either way I look at it, I have a problem. I can attempt to attach files
with Message.Attachments.Add() and streams, then start over with
Message.AddAttachment() if it fails, but that isn't a very satisfying
compromise.

Is there a small(ish) repeatable re-production for this problem or is it
more random and intermittent?
I use the technique a lot although I'm not sure I've ever attached HTML
content. What other types of content gets it upset? It could be that CDO
treats HTML as a special case for some reason.

--
Anthony Jones - MVP ASP/ASP.NET

Jul 30 '07 #6
"Anthony Jones" wrote:
Is there a small(ish) repeatable re-production for this problem or
is it more random and intermittent?
Those are not mutually exclusive options, Anthony. It's not especially small
to create an architecture to insert files into a database and to extract
them upon form submission. But the symptoms do not appear to be random or
intermittent. I can repeat it at will.
I use the technique a lot although I'm not sure I've ever attached
HTML content. What other types of content gets it upset? It could
be that CDO treats HTML as a special case for some reason.
It appears to happen for all .txt, .htm, .html, or .xml files. Change the
extension on any of them to .log or .asp, for example, and the same file
goes through without error.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Jul 30 '07 #7
I wrote:
It appears to happen for all .txt, .htm, .html, or .xml files. Change
the extension on any of them to .log or .asp, for example, and the
same file goes through without error.
You know, this got me thinking, so I looked in the database, and each of the
affected ContentType values were of the text/xxx variety. Recalling you
advice about Response.AddHeader (and some ancient memory in my head about
how Mac IE5 used to send resource forks along with uploads), I asked what
business it was of mine to tell the client how to handle the file. I removed
the Part.ContentMediaType assignment, and everything sailed through as
desired.

Seems fair, no?

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Jul 30 '07 #8
"Dave Anderson" <NP**********@spammotel.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
I wrote:
It appears to happen for all .txt, .htm, .html, or .xml files. Change
the extension on any of them to .log or .asp, for example, and the
same file goes through without error.

You know, this got me thinking, so I looked in the database, and each of
the
affected ContentType values were of the text/xxx variety. Recalling you
advice about Response.AddHeader (and some ancient memory in my head about
how Mac IE5 used to send resource forks along with uploads), I asked what
business it was of mine to tell the client how to handle the file. I
removed
the Part.ContentMediaType assignment, and everything sailed through as
desired.

Seems fair, no?
As long as the file extension of the filename in the content-disposition
matches the content of the body part the client ought to handle it ok.
Personally I would much prefer the more explicit Content-Type header be
present.

The error I get is "operation not allowed in this context". This is because
when the content type is a text type then the ADODB stream object returned
by DecodedContentStream is in text mode and therefore is expecting us to use
WriteText not Write. This allows the stream object to encode the text to
the character set being used by the message.

I missed it because I simplified my code a little too much in my example.
The actual code I use (which is a VB6 dll) is 'overloaded' and in addition
to an array of bytes it can also take a refererence to IStream. In the
latter case the ADODB stream object's default interface is by passed and its
IStream interface is used to copy from the input IStream. This also
bypasses the character encoding that ADODB stream would have wanted to do
for a text type but since the transfer encoding is base64 this doesn't cause
a problem in transfer and the resulting file extracted at the other end is
byte for byte identical to the source.

If the email charset is different from the charset used in the source file
this might be a problem when the client tries to view the attachment.
--
Anthony Jones - MVP ASP/ASP.NET
Jul 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tihon | last post by:
Hello! I again need your help, just can't understand whats going on. Got this upload pictures form and it's having problem handling large files (~1.5 - 2 MB). Everything works fine if i just...
15
by: Simon | last post by:
I would like to create a very basic file upload add image form to add to my web site and to keep them in a "tmp" directory within my web hosting file manager once uploaded. I understand the basic...
2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
2
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
7
by: pbd22 | last post by:
hi. i am having probs understanding how to grab a file being uploaded from a remote client. i am using hidden input fields for upload such as: <input id="my_file_element" type="file"...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
6
Jacotheron
by: Jacotheron | last post by:
I need a PHP script that can upload music files (mp3). The script is for a home project I have started a while ago. I have a MySQL database of all the music that I have. Other computers on the...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.