Link to home
Start Free TrialLog in
Avatar of robnhood00
robnhood00

asked on

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

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.
Avatar of robnhood00
robnhood00

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!