upload file using ajax and send as email attachment without saving to server

robnhood00
robnhood00 used Ask the Experts™
on
I'm trying to upload a file using ajax to a vb.net handler and then sending that file as an email attachment without saving the file to the server.  I've tried several examples that I've found online, however, I'm not getting anywhere.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I was able to get this to work using the code below, however, it saves the file to the server first.  Does anyone know how to code it without saving it?
Dim fileName As String = HttpContext.Current.Request.QueryString("FileName").ToString
        Dim fs As FileStream = File.Create((context.Server.MapPath("~/" + fileName)))
        Dim buffer() As Byte = New Byte(((32 * 1024)) - 1) {}
        Dim read As Integer = context.Request.GetBufferlessInputStream.Read(Buffer, 0, Buffer.Length)
        
        While (read > 0)
            fs.Write(Buffer, 0, read)
            read = context.Request.GetBufferlessInputStream.Read(Buffer, 0, Buffer.Length)
        End While

        'Mail File
        Dim myMail As New System.Net.Mail.MailMessage("web@domain.com", "test@domain.com", "test", "Attachement test")
        Dim mail As New System.Net.Mail.SmtpClient()
        myMail.Attachments.Add(item:=New Attachment(HttpContext.Current.Server.MapPath("~/" & filename)))
        myMail.IsBodyHtml = True
        mail.Send(myMail)
        myMail = Nothing
      
        fs.Close()
        fs = Nothing

Open in new window

Software Engineer
Commented:
I haven't tested this but can you try 2 options, first using a MemoryStream:
        Dim fileName As String = HttpContext.Current.Request.QueryString("FileName").ToString
        Dim buffer() As Byte = New Byte(((32 * 1024)) - 1) {}
        Dim fs As New MemoryStream(buffer)
        Dim read As Integer = Context.Request.GetBufferlessInputStream.Read(buffer, 0, buffer.Length)

        While (read > 0)
            fs.Write(buffer, 0, read)
            read = Context.Request.GetBufferlessInputStream.Read(buffer, 0, buffer.Length)
        End While

        'Mail File
        Dim myMail As New System.Net.Mail.MailMessage("web@domain.com", "test@domain.com", "test", "Attachement test")
        Dim mail As New System.Net.Mail.SmtpClient()
        myMail.Attachments.Add(item:=New Attachment(fs, fileName))
        myMail.IsBodyHtml = True
        mail.Send(myMail)
        myMail = Nothing

        fs.Close()
        fs = Nothing

Open in new window

Second, try using the posted stream directly:
        Dim fileName As String = HttpContext.Current.Request.QueryString("FileName").ToString

        'Mail File
        Dim myMail As New System.Net.Mail.MailMessage("web@domain.com", "test@domain.com", "test", "Attachement test")
        Dim mail As New System.Net.Mail.SmtpClient()
        myMail.Attachments.Add(item:=New Attachment(Context.Request.InputStream, fileName))
        myMail.IsBodyHtml = True
        mail.Send(myMail)
        myMail = Nothing

Open in new window

Author

Commented:
I tried the example above with the results below:
The First Solution
Sometimes it sent and sometimes it did not.  When it does send what appears to be a partial version of the file.  I attached a 30kb file and what was sent was a 3kb file.  

The Second Solution
Worked fine consistently.  Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial