472,352 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 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 3984
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...
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...
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...
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...
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...
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: ...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.