tags:

views:

307

answers:

1

I'm writing a program to read from a POP3 mailbox and upload the email message into my ticketing system. I would like to then take the attachments and upload them as well. I've got all the information I need except the file size.

Is there a way to determine the file size from the mail message? I am taking the attachment, decoding the base64 encoded string and breaking it into a byte array to store in database. If there is another way to determine the file size, I'm willing to try that too.

I'm working in C# and .NET 3.5.

Thanks!!!

+4  A: 

Is the size of the base64-decoded byte array correct for you? If so, all you need to do is calculate the size without decoding it, right?

A byte array of size N will encode to (N * 4)/3 bytes, always rounding up. You need to look at the last few characters of the string to work out how much to remove for rounding. Basically it should be something like:

string x = GetBase64DataFromWherever();
int size = (x.Length *3)/4;
if (x.EndsWith("="))
{
    size--;
}
if (x.EndsWith("=="))
{
    size--; // 1 will already have been removed by the if statement above
}

This is untested, but presumably you've got good test data you can try it with :)

Jon Skeet
@Jon Skeet - for the int size = (x*3)/4; should it be x.length? Thanks!
Steven Murawski
Yup, that's what I meant - sorry! Will edit.
Jon Skeet
No problem.. This seems to be working pretty well for the file size. Now I have to figure out how to get this library parses the attachment, but that's another question :) . Thanks!
Steven Murawski
and I found my other problem. Thanks for the assistance.
Steven Murawski