Temporary Flat Files

T

Thom Little

I have an ASP 3 application that I am converting to and improving with
ASP.NET. This application creates two temporary flat files and appends them
to an SMTP mail message that it then sends.

Can I use the Cache in ASP.NET for a similar purpose instead of writing to
and reading from disk on the server? Is there a better approach?

Is there an example or description that you can recommend?
 
M

Manohar Kamath

The following article contains code to create attachments, you could perhaps
modify it to use a stream instead.

http://www.eggheadcafe.com/articles/20030316.asp

I am guessing there is code somewhere that writes these flat files. So,
instead of writing the files, pass the stream to the above code. Hope that
helps.
 
T

Thom Little

That is a very interesting article and probably helpful for solving a larger
problem that I have.

I currently have the revised application written and tested to use Smtp. It
is invoked with a form from any HTTP page. The remaining issue is to create
and attach two files. I am looking for an example that allows me to attach
"files" that are not physically resident on disk. Perhaps this is a
capability provided by the Cache?

The reason is that writing them to disk makes it necessary to have a
dedicated folder (temp) that has permissions adjusted for ASP.NET access.
This makes it cumbersome to setup when moved to a server under some else's
control.
 
M

[MSFT]

Hello Thom,

The object in ASP.NET cache cannot be used as actual file for amail
attachment. I think Manohar's suggestion is the right way to resolve the
problem. You can take a look at following code in the sample he recommended:

MailAttachment a = o as MailAttachment;
byte[] binaryData;
if(a!=null)
{
FileInfo f = new FileInfo(a.Filename);
sb.Append("--unique-boundary-1\r\n");
sb.Append("Content-Type: application/octet-stream; file=" + f.Name +
"\r\n");
sb.Append("Content-Transfer-Encoding: base64\r\n");
sb.Append("Content-Disposition: attachment; filename=" + f.Name + "\r\n");
sb.Append("\r\n");
FileStream fs = f.OpenRead();
binaryData = new Byte[fs.Length];
long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
fs.Close();
string base64String = System.Convert.ToBase64String(binaryData,
0,binaryData.Length);

...

Above code add an attachment to the mail and data is in the byte array.
Based on the sample, you can load your data in a byte array or stream, and
then add to the mail.

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top